因斯福论坛

帖子
查看: 15172|回复: 0

C++析构函数

[复制链接]
发表于 2013-5-1 21:22:34 | 显示全部楼层 |阅读模式

C++析构函数:

1、对于动态对象,即指向对象的指针,用 delete <object> 的方法调用析构函数。
2、对于静态定义的对象,无需(也不能)显示调用析构函数。
3、子类自动调用父类的构造或析构函数。

#include <stdio.h>
#include <conio.h>

class base
{
public:
        base(char *Name)
        {
                this->Name=Name;
                printf("this is the base conntructor of %s\n", Name);
        }

        base()
        {
                base("Anonymous");
        }

        ~base()
        {
                printf("this is the base destructor of %s\n", Name);
        }

protected:
        char *Name;
};

class derivative : public base
{
public:
        derivative(char *Name) : base(Name)
        {
                printf("this is the derivative conntructor of %s\n",Name);
        }

        ~derivative()
        {
                printf("this is the derivative destructor of %s\n", Name);
        }
};

void test()
{
        derivative *a=new derivative("a");
        derivative b("b");
        delete a;
}

int main(int argc, char* argv[])
{
        test();
        getch();
        return 0;
}

回复

举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|因斯福论坛  

GMT+8, 2025-5-25 03:03

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表