Advertisement

C++ quickie.

Started by January 01, 2001 04:09 PM
4 comments, last by Promiscuous Robot 24 years ago
Just a quick C++ question: If I have derived from a base class and made the destructor virtual like so: class A { public: A (); virtual ~A(); }; class B : public A { public: B(); ~B(); }; When I delete an object of type B, will the destructor for A also be called? In other words will this: B* obj = new B; delete obj; do this: // allocate mem for obj. B(); // call object constructor. ~B(); ~A(); // deallocate obj from heap. Or do I have to explicitly call it from the derived destructor, similar to B::B() : A() for the constructor? I hope that makes sense.....
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
I am almost sure that when you delete something its deconstructor is called. To test this have the deconstructor write something to the screen when it is called, then tell then run the program and see if the text is written to the screen when the object is deleted.

Matthew
WebMaster
www.Matt-Land.com
It is foolish for a wise man to be silent, but wise for a fool.
Advertisement
Of course the destructor is called, I''m just wondering if all the destructors in a inheritance chain are called.

Say B is derived from A. A has a virtual destructor.
If I delete object of class B using a pointer to A, then B''s destructor will be called.

However, will the destructor for the base class A be called as well?
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Yes it is called.
You could use a debugger to convince of this
The dtor will only get called if the destructor in the base class
is declared virtual (annoying isn''t it? my C++ annoyance #1 is this type of issue, the language forces the base class programmer to make such decisions that he shouldn''t have to make since these are things that the compiler should easily be able to deduce > .. (this is ironically amusing coming from me, who is a former C++ junkie/lover ) ..

in fact the dtor''s get called in the reverse order of the inheritance chain ..
e.g.

#include
using namespace std;

class A
{
public:
A() {cout<<"A made\n";}
virtual ~A() {cout<<"A destroyed\n";}
};

class B : public A
{
public:
B() {cout<<"B made\n";}
virtual ~B() {cout<<"B destroyed\n";}
};

class C : public B
{
public:
C() {cout<<"C made\n";}
~C() {cout<<"C destroyed\n";}
};

int main ()
{
C c;
return 0;

}
this is what will get outputted:
A made
B made
C made

C destroyed
B destroyed
A destroyed

--vat
Einstein on Quantum Mechanics:"GOD does not play dice with the universe."
Hawking on Einstein:"GOD not only plays dice with the universe, he tosses them where we can''t see the results."
--
Srivatsan Raghavan
machinshin@onebox.com - email


grr.. i guess that the gamedev forums directly read html syntax
directly (forgot about that)
so, as a note,
#include <iostream>

goes at the top of the code

btw, you don''t *HAVE* to call the base class constructor
all the time, you only call it if you have to pass parameters
to the ctor of the base class (to init base class members)
in this case you don''t have any parameters so the call to the
the class A ctor happens implicitly .. (just a C++ side note )


--vat
Einstein on Quantum Mechanics:"GOD does not play dice with the universe."
Hawking on Einstein:"GOD not only plays dice with the universe, he tosses them where we can''t see the results."
--
Srivatsan Raghavan
machinshin@onebox.com - email



This topic is closed to new replies.

Advertisement