Don't use print, use cout.
Yes, I know that. This was a quick and dirty example to illustrate a point.
The code that I'm having trouble with is big and complex. But I'll strip out the stuff that isn't necessary. Here goes:
Code:
class Control
{
public:
Control();
void update();
addControl(Control *);
update();
private:
virtual void draw();
virtual void initialize();
};
class Window : public Control
{
public:
Window();
~Window();
private:
void draw();
void initialize();
std::string mTitlebar;
int mTitlebarHeight;
};
class DebugWindow : public Window
{
public:
DebugWindow();
~DebugWindow();
private:
void initialize();
};
In particular the trouble shows up when I run the addControl() function:
Code:
void Control::addControl(Control *control)
{
mChildControls.push_back(control);
mChildControls.back()->initialize();
}
mChildControls.back()->initialize() only ever gets called once and only runs the initialize() function from the class Control, not Window or DebugWindow.
The update function looks like this:
Code:
void Control::update()
{
draw();
for(unsigned int i = 0; i < mChildControls.size(); i++)
mChildControls[i]->update();
}
This function tells the Control to draw itself and then to iterate through all of its children and tells them to update themselves which in turn calls their update and so on... Now, this works. The correct draw() function is called.
I may have just answered my own question as to why so now the question has turned into how. But just in case, I'm still asking...
mChildControls is defined as a vector<Control*>. There are probably a few things here and there that don't appear to be defined but just note that I've stripped out all of the irrelevant functions/variables/etc.