|
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;
}
|
|