C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-21-2009, 03:42 AM   #16
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
I believe the rule is that if you have a virtual function, the destructor should be virtual since it implies you are going to use polymorphism. Inheritance does not necessarily mean you are going to use polymorphism, thus virtual destructors are usually only an overhead.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-21-2009, 06:00 AM   #17
Hail to the king, baby.
 
Akkernight's Avatar
 
Join Date: Oct 2008
Location: Faroe Islands
Posts: 713
Well, I dunno exactly what virtual means, but it doesn't change anything serious, if placed wrong, right?
__________________
I deny the Holy Spirit; burn me.

http://blasphemychallenge.com/ <- Do you dare?
Akkernight is online now   Reply With Quote
Old 02-21-2009, 06:11 AM   #18
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Best read up about inheritance, polymorphism and virtual functions, as well as abstract base classes (pure virtual functions).
Not using virtual correct can mean overhead and headaches.

Also, while deriving classes will "inherit" the virtual status of functions (including destructors), it is good practice, methinks, to actually mark them as virtual anyway, because it may not be obvious they are virtual unless you take a look at the base class and it doesn't hurt to put virtual in there anyway.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.

Last edited by Elysia; 02-21-2009 at 06:26 AM.
Elysia is offline   Reply With Quote
Old 02-21-2009, 06:21 AM   #19
Registered User
 
Join Date: Jan 2008
Posts: 575
Yes, it will.

Despite what has been offered, you only need a virtual destructor if you are going to destroy an object polymorphically. (If you religiously use good smart pointers, it is really a non-issue, but still, rather safe than sorry.) However, if you get this wrong, you destroy the universe. Consequently, if you already have one virtual method, marking the destructor as virtual will save you a headache and probably cost you only a little overhead--probably only measurable in hundreds of thousands of iterations or tens of bytes.

By the by, it is always the "handle" class that needs a virtual destructor. (Where the "handle" class is whatever type you will store a pointer to for the sake of polymorphic destruction.) The destructor for classes that inherit from the "handle" class do not need to be marked 'virtual'.

Soma

Last edited by phantomotap; 02-21-2009 at 06:25 AM.
phantomotap is offline   Reply With Quote
Reply

Tags
classes, pointer

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Q: Pointers to Classes '->' notation Howie17 C++ Programming 2 12-12-2008 10:09 AM
destructors and parameters + pointers to classes questions. Zigs C++ Programming 12 12-31-2007 06:59 AM
two-dimensional dynamic array of pointers to classes Timo002 C++ Programming 4 04-21-2005 06:18 AM
Pointers to Classes || pointers to structures C++Child C++ Programming 24 07-30-2004 06:14 PM
Help With pointers in classes. indigo0086 C++ Programming 12 10-10-2002 02:03 PM


All times are GMT -6. The time now is 08:06 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22