|
派生类的构造函数应在初始化表里调用基类的构造函数; 派生类和基类的析构函数应加Virtual关键字。 不要小看构造函数和析构函数,其实编起来还是不容易。 #include <iostream.h> class Base { public: virtual ~Base() { cout<< "~Base" << endl ; } }; class Derived : public Base { public: virtual ~Derived() { cout<< "~Derived" << endl ; } }; void main(void) { Base * pB = new Derived; // upcast delete pB; } 输出结果为: ~Derived ~Base 如果析构函数不为虚,那么输出结果为 ~Base 本文转载自IT网it求职笔试真题库网。
注:转载文章需注明来源: VCer.net 文章地址: http://vcer.net/1000000000014.html
如果你觉得VCer.net不错,而且你愿意为VCer.net捐赠一元钱,那么点击后面的捐赠按钮吧:)
|