gpt4 book ai didi

c++ - 通过基指针获取派生类?

转载 作者:太空宇宙 更新时间:2023-11-04 15:51:02 25 4
gpt4 key购买 nike

当我的对象在堆上而不是在堆栈上声明时,我可以发誓这在前一段时间有效。我有一个函数,它接受一个指向基类的指针( detail::DuplicateFn )。 - 虽然这是一个虚拟类,但实际指针由派生类给出(即 dteail::SkipFn)

1>GMProject.cpp(298): error C2664:
'void xml_tree<V>::combine_if<bool(__cdecl *)(const T &,const T &)>(const xml_tree<V> &,Predicate,const detail::DuplicateFn *)' :
cannot convert parameter 3 from 'detail::SkipFn (__cdecl *)(void)' to 'const detail::DuplicateFn *'

我的功能:

void GMProject::CombineTree(const pTree& Other) {
detail::SkipFn foo();
ProjectTree.combine_if(Other, &SimilarTreeValue<GMProject::pTree>, &foo);
}

其中 ProjectTree.combine_if() 作为第三个参数需要指向“DuplicateFn”的指针 - 而 SkipFn 派生自 DuplicateFn。

如前所述,如果我在堆上声明“foo”,它就可以正常工作(如我所料)。然而,当在堆栈上声明 foo 时(或 - 最终 - 使 foo 成为临时的),它不会。这是为什么?

最佳答案

detail::SkipFn foo();

foo 不是对象。这是一个返回类型为 detail::SkipFn 的函数。所以,改为尝试 -

detail::SkipFn foo;   // Notice the removal of (). Google most vexing parse 
// for explanation.

Is there any difference between List x; and List x();?

关于c++ - 通过基指针获取派生类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8285322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com