[英]why does pointer to member conversion from base to derived gives error
If conversion from pointer to Base member To pointer to Derived class member is valid, why does the following code fails to compile 如果从指针到基本成员的指针到指向派生类成员的指针的转换有效,为什么以下代码无法编译
class Base
{
public:
virtual void fun1()
{
cout<<"fun1 in Base"<<endl;
}
};
class Der
{
public:
void fun1()
{
cout<<"fun1 in Der"<<endl;
}
};
int main()
{
void (Der::*funptr)() = &Base::fun1;
}
Compiler gives an error saying 编译器给出错误提示
error: cannot convert 'void (Base:: )()' to 'void (Der:: )()' in initialization|
错误:初始化时无法将'void(Base :: )()'转换为'void(Der :: )()'|
Because your Der
is not derived from Base
. 因为您的
Der
不是从Base
派生的。 Your classes are unrelated. 您的课程无关。 There's no inheritance relationship between them.
它们之间没有继承关系。
If you indeed define your Der
as a descendant of Base
如果确实将
Der
定义为Base
的后代
class Der : public Base
{
...
the code will compile. 代码将编译。
The compiler cannot magically guess that you wanted to derive your Der
from Base
. 编译器无法神奇地猜测您要从
Base
派生Der
。 You are supposed to remember to explicitly tell the compiler about it. 您应该记住要明确地告诉编译器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.