43、
44、虚析构函数
#include<iostream.h>
class A
{
public:
A() { cout<<"AAAAAAAAAAAA"<<endl; }
~A() { cout<<"A::~A() called."<<endl; }
};
class B:public A
{
public:
B() { cout<<"BBBBBBBBBBBBB"<<endl; }
B(int i) { buf=new char[i]; cout<<"BBBBBBBBBBBBB"<<endl;}
~B()
{
delete buf;
cout<<"B::~B() called."<<endl;
}
private:
char *buf;
};
void fun(A *a)
{
delete a;
}
void main()
{
A *a=new B(15);
fun(a);
}
总结:a.在函数
44、虚析构函数
#include<iostream.h>
class A
{
public:
A() { cout<<"AAAAAAAAAAAA"<<endl; }
~A() { cout<<"A::~A() called."<<endl; }
};
class B:public A
{
public:
B() { cout<<"BBBBBBBBBBBBB"<<endl; }
B(int i) { buf=new char[i]; cout<<"BBBBBBBBBBBBB"<<endl;}
~B()
{
delete buf;
cout<<"B::~B() called."<<endl;
}
private:
char *buf;
};
void fun(A *a)
{
delete a;
}
void main()
{
A *a=new B(15);
fun(a);
}
总结:a.在函数