hello every body,
I know I shouldn't call virtual functions during construction or destruction.
If replace the BaseWithPureFun ction function :
I puzzled why call pure-virtual directly, the programe won't cause error.I found some explanation in "Effective C++ Item 9".But I still think that this explanation is not detailed enough.
I know I shouldn't call virtual functions during construction or destruction.
Code:
#include <iostream> using namespace std; class BaseWithPureFunction { public: virtual void PureFunc() = 0; void CallPureFunc() { PureFunc(); } BaseWithPureFunction() ; }; BaseWithPureFunction::BaseWithPureFunction() { /* Call pure-virtual via CallPureFun. Cause r6025 error.I have no question here. */ CallPureFunc(); } void BaseWithPureFunction::PureFunc() { cout<<"Pure!"<<endl; } class BaseEx:public BaseWithPureFunction { public: virtual void PureFunc() { printf( "BaseEx::PureFunc()\r\n "); } }; int _tmain(int argc, _TCHAR* argv[]) { BaseEx ex; return 0; }
If replace the BaseWithPureFun ction function :
Code:
BaseWithPureFunction::BaseWithPureFunction() { /* Call pure-virtual directly. Running well.I puzzled. */ PureFunc(); }
Comment