Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

virtual destructor

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George - 18 Oct 2007 13:16 GMT
Hello everyone,

What is the purpose of virtual destructor? If currently, no derived class?

thanks in advance,
George
Ben Voigt [C++ MVP] - 18 Oct 2007 15:32 GMT
> Hello everyone,
>
> What is the purpose of virtual destructor? If currently, no derived class?

Guarantees that if a derived class is ever defined, it can be destroyed
polymorphically.

Virtual destructors aren't needed in every circumstance, but it's a good
idea to use one if in doubt, because the cost is small (the memory manager
cost to destroy an object is larger than the cost of the indirect function
call).  However, if you haven't any other virtual functions, your object
can't be used polymorphically and you don't need a virtual destructor.
George - 18 Oct 2007 16:31 GMT
Thanks Ben,

I think you mean if the destructor is virtual, then if we delete an object
of derived class through based class pointer, then the derived class
destructor is invoked? Right?

If the destructor is non-virtual, then if we delete an object of derived
class through based class pointer, then the base class destructor is invoked?
Right?

regards,
George

> > Hello everyone,
> >
[quoted text clipped - 8 lines]
> call).  However, if you haven't any other virtual functions, your object
> can't be used polymorphically and you don't need a virtual destructor.
Ben Voigt [C++ MVP] - 18 Oct 2007 16:38 GMT
> Thanks Ben,
>
> I think you mean if the destructor is virtual, then if we delete an object
> of derived class through based class pointer, then the derived class
> destructor is invoked? Right?

Correct.  Base class destructors will be automatically invoked in the
correct order as well according to the object lifetime rules.

> If the destructor is non-virtual, then if we delete an object of derived
> class through based class pointer, then the base class destructor is
> invoked?
> Right?

That's what will most likely happen in real life, but I think the standard
simply says "undefined behavior".  Object lifetime rules are being violated
and the implementation is allowed to do anything it wants, including being
totally inconsistent.
George - 18 Oct 2007 16:51 GMT
Thanks Ben,

> Correct.  Base class destructors will be automatically invoked in the
> correct order as well according to the object lifetime rules.

You mean if the destructors are virtual, even if they do not have explicit
invocation relationship in code (e.g. the most derived destructor will call
the second-last destructor explicitly in source codes), the second-last
destructor, the third-last destructor and so forth will be invoked
automatically?

(If we delete an object with a virtual destructor, through a base instance,
the most derived destructor will be called first, then the second-last
destructor, the third-last destructor and so froth -- right up to the base
destructor. All are automatically invoked without invocation relationship
explicitly in source codes)

But if the destructor is not virtual, we do not have this feature?

regards,
George
David Wilkinson - 18 Oct 2007 18:52 GMT
> Thanks Ben,
>
[quoted text clipped - 14 lines]
>
> But if the destructor is not virtual, we do not have this feature?

George:

No. All destructors have this feature, even when they are not virtual.
But having a virtual destructor ensures that the process always starts
with the most derived constructor.

Signature

David Wilkinson
Visual C++ MVP

George - 19 Oct 2007 06:39 GMT
Thanks David,

regards,
George

> > Thanks Ben,
> >
[quoted text clipped - 20 lines]
> But having a virtual destructor ensures that the process always starts
> with the most derived constructor.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.