Stupid N0ob question from somebody who should know better

Started by
16 comments, last by demonkoryu 18 years, 9 months ago
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?
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
Advertisement
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
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!
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
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
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
thanks!
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
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
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
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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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.
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
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."

This topic is closed to new replies.

Advertisement