关闭

关于析构函数

标签: destructorconstructorclassdelete
678人阅读 评论(0) 收藏 举报
分类:

注意:#include <iostream.h>

class Base

{

public:

       Base(){cout<<"Base Constructor!"<<endl;}

        ~Base(){cout<<"Base Destructor!"<<endl;}

};

 

class Derived: public Base

{

public:

       Derived(){cout<<"Derived Constructor!"<<endl;};

       ~Derived(){cout<<"Derived Destructor!"<<endl;};

};

 

void main()

{

       Derived *pb;

       pb = new Derived;

       delete pb;

}

结果:

Base Constructor!

Derived Constructor!

Derived Destructor!

Base Destructor!

 

#include <iostream.h>

class Base

{

public:

       Base(){cout<<"Base Constructor!"<<endl;}

        ~Base(){cout<<"Base Destructor!"<<endl;}

};

 

class Derived: public Base

{

public:

       Derived(){cout<<"Derived Constructor!"<<endl;};

       ~Derived(){cout<<"Derived Destructor!"<<endl;};

};

 

void main()

{

       Base *pb;

       pb = new Derived;

       delete pb;

}

结果:

Base Constructor!

Derived Constructor!

Base Destructor!

 

如果基类的析构函数为virtual ~Base,则结果为

Base Constructor!

Derived Constructor!

Derived Destructor!

Base Destructor!

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:185262次
    • 积分:703
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:11篇
    • 译文:0篇
    • 评论:120条
    最新评论