在构造函数/析构函数中能否调用虚函数?
Posted 修修55
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在构造函数/析构函数中能否调用虚函数?相关的知识,希望对你有一定的参考价值。
class Base { public: Base() { Fuction(); } virtual void Fuction() { cout << " construct Base::Fuction" << endl; } ~Base() { desFuction(); } virtual void desFuction() { cout << " destruct Base::Fuction" << endl; } }; class A : public Base { public: A() { Fuction(); } ~A() { desFuction(); } virtual void Fuction() { cout << " construct A::Fuction" << endl; } virtual void desFuction() { cout << " destructr A::Fuction" << endl; } }; int test() { A a; return 0; }
输出结果:
cout << " construct Base::Fuction" << endl;
cout << " construct A::Fuction" << endl;
cout << " destructr A::Fuction" << endl;
cout << " destruct Base::Fuction" << endl;
以上是关于在构造函数/析构函数中能否调用虚函数?的主要内容,如果未能解决你的问题,请参考以下文章