Stupid N0ob question from somebody who should know better

Started by
16 comments, last by demonkoryu 18 years, 9 months ago
Quote:Original post by Shannon Barber
If you have any virtual methods (pure or not) the dtor should also be virtual (this will be automatic in C++0x)


Quoted for agreement.

There are no virtual constructors, and you should always make your base class destructors virtual. If you don't, the base class slice of your object will not be destroyed, if the derived class destructor is called.

(!)
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Advertisement
Ok, so now, I need to make the base destructor virtual?
Can it still have code in it, if it's virtual?

@Mumpo and Shannon:
This 2D library I am rewriting requires some very specific window constraints, such as it can't be resizable, it can be fullscreen, it can have a caption, and it can have a variable depth. Also, They don't need to allocate any memory to the depth buffer as well. The window and OpenGL environment variables are so specific that the user shouldn't have to see it.

Right now, I am only coding an SDL context and image factory, but in the future, I will be creating a pure Win32 one, and others will be able to make contexts for the Apple Mac platform as well, or if somebody is posessed enough, AllegroGL or GLFW.

Why not just pure SDL and be done with it? I don't use LGPL, I use Zlib. This is not a derivitive work, but is compatable instead. If I have multiple interfaces, or atleast the option for multiple interfaces, I don't have to worry about clashing licenses.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
Quote:Original post by PnP Bios
Ok, so now, I need to make the base destructor virtual?
Can it still have code in it, if it's virtual?

yes. if the base class destructor isn't virtual it won't get called.

edit: bjarne stroustrup's brief explanation
This space for rent.
Quote:Original post by PnP Bios
Ok, so now, I need to make the base destructor virtual?
Can it still have code in it, if it's virtual?


Yes. In fact, that's where you should destroy any base class owned data, if necessary.


my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Quote:Original post by Verg
[...] and you should always make your base class destructors virtual. If you don't, the base class slice of your object will not be destroyed, if the derived class destructor is called.


Nitpick: It's actually the other way around. The derived destructor won't get called if the base destructor is called.

If you can guarantee that no one will ever delete a base pointer to a derived instance (ofcourse you almost never can [wink]) the virtual destructor is not needed.
___________________________Buggrit, millennium hand and shrimp!
Quote:Original post by samv
Quote:Original post by Verg
[...] and you should always make your base class destructors virtual. If you don't, the base class slice of your object will not be destroyed, if the derived class destructor is called.


Nitpick: It's actually the other way around. The derived destructor won't get called if the base destructor is called.

If you can guarantee that no one will ever delete a base pointer to a derived instance (ofcourse you almost never can [wink]) the virtual destructor is not needed.


*nods*
PNP > may be the best way to get this straight in your own mind would be looking at the ogre3d source or something - because that has supporting uml documents and code that show abstraction away from a particular rendering system (opengl, dx7, dx9 I believe).
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Another "funny" fact that is very important in this context:

Calling virtual functions in the constructor or destructor will most likely NOT do what you want. (This is also discussed in Stroustrups FAQ mentioned above by gumpy macdrunken)
___________________________Buggrit, millennium hand and shrimp!
Guru of the Week has a great explanation on the order of construction.

This topic is closed to new replies.

Advertisement