设为首页 加入收藏

TOP

c++虚析构函数
2017-06-16 10:22:35 】 浏览:5668
Tags:函数

c++虚析构函数
 

#include <iostream>
using namespace std;
class Base
{
private:
    int* a;
public:
    Base(){
        cout<<"base construct new int"<<endl;
        a = new int();
    }
    //必须申明为虚析构函数,不然子类的析构函数将不会被执行
    virtual ~Base(){
        cout<<"base destruct del int"<<endl;
        delete a;
    }
};
class Child : public Base{
private:
    int* b;
public:
    Child(){
        cout<<"Child construct new int"<<endl;
        b = new int();
    }
    ~Child(){
        cout<<"Child destruct del int"<<endl;
        delete b;
    }
};
int main()
{
    Base* b;
    b = new Child();
    delete b;
}

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇MFC延时功能GetTickCount的实现 下一篇c++装饰模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目