M
Miri
Hi.
I have the following program:
class Papa
{
public:
Papa() {cout<<"Papa "<<endl;}
~Papa() { cout<<"~Papa "<<endl;}
};
class Son : public Papa
{
public:
Son() { m_pNumber=new int[100]; cout<<"Son "<<endl;}
~Son() { cout<<"~Son "<<endl;delete[] m_pNumber; }
private:
int* m_pNumber;
};
void main()
{
try{
Papa* p = new Son;
Son* a = new Son;
Son* b = a;
delete a;
delete b;
}
catch(...){
cout<<"Ooooooooops..."<<endl;
}
}
It is obvious that we get run time error when doing "delete b" since
we already deleted m_pNumber.
Why do we continue to Papa's destructor as well?
Thanks.
I have the following program:
class Papa
{
public:
Papa() {cout<<"Papa "<<endl;}
~Papa() { cout<<"~Papa "<<endl;}
};
class Son : public Papa
{
public:
Son() { m_pNumber=new int[100]; cout<<"Son "<<endl;}
~Son() { cout<<"~Son "<<endl;delete[] m_pNumber; }
private:
int* m_pNumber;
};
void main()
{
try{
Papa* p = new Son;
Son* a = new Son;
Son* b = a;
delete a;
delete b;
}
catch(...){
cout<<"Ooooooooops..."<<endl;
}
}
It is obvious that we get run time error when doing "delete b" since
we already deleted m_pNumber.
Why do we continue to Papa's destructor as well?
Thanks.