By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,116 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,116 IT Pros & Developers. It's quick & easy.

Why for the Virtual Destrcutors

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

Aug 3 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

sabarish wrote:
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

Aug 3 '05 #2

P: n/a
sabarish wrote:
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-

Aug 3 '05 #3

P: n/a
> 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
Aug 3 '05 #4

P: n/a
WittyGuy wrote:
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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 3 '05 #5

P: n/a

"benben" <be******@hotmail.com> wrote in message
news:42***********************@news.optusnet.com.a u...
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...


.... 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
Aug 3 '05 #6

P: n/a
Pete Becker wrote:
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-

Aug 4 '05 #7

P: n/a
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..

"Pete Becker" <pe********@acm.org> wrote in message
news:HI********************@rcn.net...
WittyGuy wrote:
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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Oct 2 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.