关于下面的C++程序:
class Base { };
class Child : public Base { };
int main()
{
// Normal: using child as base is allowed
Child *c = new Child();
Base *b = c;
// Double pointers: apparently can't use Child** as Base**
Child **cc = &c;
Base **bb = cc;
return 0;
}
GCC在最后一条赋值语句上产生以下错误:
error: invalid conversion from âChild**â to âBase**â
我的问题分为两部分:
-
为什么没有从child**到base**的隐式转换?
-
我可以使用C样式的强制转换或
reinterpret_cast
. 使用这些石膏意味着扔掉所有类型的安全。我可以在类定义中添加什么来使这些指针隐式强制转换,或者至少用允许我使用的方式表达转换
static_cast
相反?