我有一个类层次结构如下:
class Base
{
public:
virtual Base& derived() const=0;
}
class Derived:public Base
{
public:
Derived& derived() const
{
return dynamic_cast<Derived&>(*this);
}
void test(){cout<<"Hi";}
}
的目的derived()
是返回确切类型的引用。
int main()
{
Derived d;
Base &b = d;
(b.derived()).test();
return 0;
}
我得到一个编译错误
can't find void test() in Base
当我检查时,Derived::derived()
确实被调用了。似乎Derived::derived()
没有按预期返回对派生类的引用。