Why for the Virtual Destrcutors

S

sabarish

Hi to all. Please i can't get information why the Virtual Destructors
and for what purpose they are.
 
D

Dan Cernat

sabarish said:
Hi to all. Please i can't get information why the Virtual Destructors
and for what purpose they are.

To prperly destroy objects when they are deleted via a pointer to the
base class.


Example:

class Base
{
public:
virtual ~Base() {}
};

class Derived : public Base
{
public:
virtual ~Derived() {}
};

int main()
{
Base* b = new Derived;

delete b; // <- this will invoke the Derived destructor

return 0;
}

//dan
 
W

WittyGuy

sabarish said:
Hi to all. Please i can't get information why the Virtual Destructors
and for what purpose they are.

Virtual Destructor is mainly used whenever the object needs to be
completeltly deleted. That is without virtual destructor, the base
class destructor alone will be invoked which deletes base class part
alone which is not our intention. The derived class objects allocation
has also to be deleted which is only possible using virtual destructor.

-Wg-
 
B

benben

Hi to all. Please i can't get information why the Virtual Destructors
and for what purpose they are.

To properly destroy an object destructors must be called from the outside
in, i.e. from the most derived object towards the base(s) recursively.
Virtual destructor ensures that a call to the destructor from one of the
bases is dispatched to the most derived class's destructor.

ben
 
P

Pete Becker

WittyGuy said:
That is without virtual destructor, the base
class destructor alone will be invoked which deletes base class part
alone which is not our intention.

That is often what happens, but technically the behavior is undefined.
So don't rely on this happening.
 
H

Howard

benben said:
To properly destroy an object destructors must be called from the outside
in, i.e. from the most derived object towards the base(s) recursively.
Virtual destructor ensures that a call to the destructor from one of the
bases is dispatched to the most derived class's destructor...

.... when deleting an object via a pointer-to-base-class. (If you're using a
pointer-to-derived-class, or if an automatic object of derived-class type is
destroyed, then the virtual destructor isn't required.)

-Howard
 
W

WittyGuy

Pete said:
That is often what happens, but technically the behavior is undefined.
So don't rely on this happening.

Technically speaking, could you tell me where the member variables will
be residing for different specifiers? How then, data hiding is
implemented?

thanks
-Wg-
 
D

Dave Townsend

Surely the behavior is defined, the derived part of the object is not
destroyed,
so you have memory resource leaks, or is there something else? Ok, I guess
if the derived class managed some thread, the thread may still be running
after
the object was "destroyed" and now could try to access something in the base
class
which is now gone..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top