I thought I understood inheritance but it turns out I don't.
I am creating an SDL/OpenGL mini-GUI library.
I have a master class, GUIComponent, with subclasses such as TextLabel under it.
I have a vector called guiList that is initialized as
std::vector<GUIUnit*> guiList (it's extern'd and initialized elsewhere; it's not a visibility problem). It contains pointers to a custom class called GUIUnit.
These GUIUnits have a function,
getComponent(int) , that retrieves the argument's indexed value from a vector of pointers to GUIComponents. That too, works.
I have a subclass of GUIComponent called TextLabel. I can add them to the vector with no problem. However, I want to use the call
guiList->getComponent(1)->getFont() to get a %s-thingy that I can use for printf() debugging output. However, I get a compilation error that the class GUIComponent has no member getFont(). However, TextLabel does, and getComponent(1) returns a GUIComponent that is actually a TextLabel.
If anybody can understand me I have two questions.
- Is this legal? (For example, can I set a GUIComponent as the return of a function and instead return a subclass?)
- What would work?
- Why is the compiler complaining?
Thank you very much in advance and have a happy New Year.