Sign in to follow this  
PnP Bios

Stupid N0ob question from somebody who should know better

Recommended Posts

PnP Bios    494
Here is the problem, I am writing a graphics library over OpenGL, and it is going to be class based. The thing is that I am going to make it so that you can create a context from SDL, Win32, AllegroGL, or whatever else people choose to port this to. So, the obvious choice is to have the context class be puerly virtual, right? Each derivitave class would be able to overide the CreateWindow member, with it's own code, and everybody would be happy, right? Well, what about stuff like the screen width and height, and the stuff that is pure OpenGL, and can be re-used? It doesn't make sense to have all the members be virtual... So, you should be able to do half the members being virtual, and the other half being normal, right? The area of confusion for me comes to the constructor and destructor... what happens to those when I have a half virtual half non-virtual class? If I define them as virtual, and make code for them in the root class, will it still get called? Do I have to explicitly call the root class constructor from one of the derived class?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The existance of a pure virtual method doesn't change a thing. The constructor and destructor will both be called automatically, and under the same assumptions, with or without the pure virtual method.

Logically, the ability to add private data members to your quasi-virtual base class requires this behavior. If the constructor didn't get called, then nobody would be able to create those members.

CM

Share this post


Link to post
Share on other sites
PnP Bios    494
Ok, so I don't have to worry about declaring the constructor and destructor as virtual in the base class, and they will still get called automaticaly.
And I just delcare the stuff virtual that isn't going to be touched by the base class? Ok then, thanks a bunch!

Share this post


Link to post
Share on other sites
Ademan555    361
As long as the constructor isnt virtual, it will be called like so

parent class constructor
child class constructor
child of child class constructor
child of child of child class constructor

in the destructor i believe its the reverse, starting with the "lowest" on the tree, and ending at the parent, or root.

(im PRETTY SURE about this, but im not completely positive)

(btw, in the case that the constructor or destructor is virtual, it will get overridden and the base class constructor would NOT be called to the best of my knowledge)

hope that helps and is accurate (im pretty sure, but you may want a second opinion haha)

cheers
-Dan

Share this post


Link to post
Share on other sites
Conner McCloud    1135
Quote:
Original post by PnP Bios
Ok, so I don't have to worry about declaring the constructor and destructor as virtual in the base class, and they will still get called automaticaly.
And I just delcare the stuff virtual that isn't going to be touched by the base class? Ok then, thanks a bunch!

I've never seen anything requring a virtual constructor. However, if you plan on storing pointers to this base class, you do need a virtual destructor. Otherwise, it won't propagate back up the chain properly.

CM

Share this post


Link to post
Share on other sites
Shannon Barber    1681
If you have any virtual methods (pure or not) the dtor should also be virtual (this will be automatic in C++0x).

Let the user use whatever they want to get the app up and running (SDL, Win32, wxWidgets, etc...) and let them activate the correct OGL context. You don't have to worry about it, and it'll just work.

Share this post


Link to post
Share on other sites
mumpo    534
If you can't interface with the graphics library the same way regardless of what's underneath, why bother trying to write it to allow different renderers/windowing systems at all? You should either come up with a standard interface that will work for all of the renderers you want to support, or not waste your time trying to half-support them.

EDIT: Oh yeah, and if I am just not thinking clearly and what you are trying to do makes sense somehow... You can do that; just make sure that the destructors are virtual. Other than that you don't have to do anything special.

Share this post


Link to post
Share on other sites
Ademan555    361
Quote:
Original post by moagstar
Quote:
Original post by Ademan555
As long as the constructor isnt virtual, it will be called like so


You can't make a constructor virtual in c++, to simulate this functionality you have to use the Virtual Constructor Idiom


Well, there yah go, my innaccuracies lol. I've never had a use for virtual constructors or destructors so i never found that out haha

cheers
-Dan

Share this post


Link to post
Share on other sites
Verg    450
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.

(!)

Share this post


Link to post
Share on other sites
PnP Bios    494
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.

Share this post


Link to post
Share on other sites
Verg    450
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.


Share this post


Link to post
Share on other sites
samv    312
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.

Share this post


Link to post
Share on other sites
paulecoyote    1065
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).

Share this post


Link to post
Share on other sites
samv    312
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)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this