Rule9:绝不在构造和析构过程中调用Virtual函数

来源:互联网 发布:淘宝客付费方式 编辑:程序博客网 时间:2024/05/03 06:55

Never call Virtual functions during construction or destruction。
因为在构造函数中调用Virtual函数,会导致继承类不能正常调用其多态函数,因为这个时候先是初始化的基类。

 class base  {  public:      base()      {          method();      }      virtual void method()      {          cout<<"this is the base method"<<endl;      }  };  class derived : public base  {  public:      void method(){        cout<<"this is the derived method"<<endl;      }};int _tmain(int argc, _TCHAR* argv[]){    derived A; //调用的是 base method    base* B = &A;    B->method();    getchar();    return 0;}
0 0
原创粉丝点击