Remeber first: virtual table's construction isn't finished before the class's constructor, so the polymorphism is unavailable during the construction of the class.
e.g
class Base { public: Base() { sayHello(); } virtual void sayHello() { cout << "Base says hello\n"; } };
class Derived : public Base { public: void sayHello() { cout << "Derived says hello\n"; } };
int main(int argc, char *argv[]) { Derived d; return 0; }
输出结果: Base says hello
|
阅读(187) | 评论(0) | 转发(0) |