【发布时间】:2012-03-30 14:28:03
【问题描述】:
可能重复:
Calling virtual method in base class constructor
Calling virtual functions inside constructors
如何从 C++ 中的构造函数调用受保护的虚方法?
class Foo
{
Foo(){
printStuff(); // have also tried this->printStuff()
}
protected:
virtual void printStuff() {}
}
class ExtendedFoo : public Foo {
protected:
virtual void printStuff() { cout << "Stuff" << endl;}
}
...
ExtendedFoo exFoo; // should print "Stuff"
【问题讨论】:
标签: c++