class Base
{
public:
virtual void f() { cout << "Base::f()" << endl; }
protected:
Base(Base* pData) : m_pData(pData) { m_pData->f(); } // 为什么输出Base::f()?为什么不是Derived::f或者直接crash?vtable这时候初始化好了吗?
Base* m_pData;
};
class Derived : public Base
{
public:
Derived() : Base(this) {}
virtual void f() { cout << "Derived::f()" << endl; }
};
int main()
{
Derived* pd = new Derived();
delete pb;
return 0;
}
这样用也可以的!
最新推荐文章于 2024-05-07 13:50:01 发布