ko***@cs.biu.ac.il (Miri) wrote in message news:<d0**************************@posting.google. com>...
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()
ISO C++ requires that main return int (even if you don't want to
return a value).
{
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
Why do we continue to Papa's destructor as well?
Deleting anything twice elicits undefined behavior, which could mean
any behavior at all. If you're lucky, the program will crash. If
you're not, your program may behave as if nothing happened (as is the
case here), or it may behave badly.
Furthermore, the operators delete and delete[] are not allowed to
throw an exception. If one diagnoses an error, beyond outputting an
error message, all it can do is ignore the error or abort the program.
Your implementation chose to ignore it.
The solution in your case is not to allow two pointers which might
share ownership of an object. My personal rule, whenever possible, is
to use "smart pointers" to manage single objects and std::vector to
manage arrays. That way, I don't actually delete anything myself, so
it's difficult to delete things too many -- or too few -- times.
- Shane