Jump to content
  • Advertisement
Sign in to follow this  
PnP Bios

Stupid N0ob question from somebody who should know better

This topic is 4796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!