I have been working on a few classes for a 2D gui system and I have a really wierd problem that I have spent days trying to work out and I really don't know what is wrong.
I have a cGUIManager class, a cBaseWindow class from which window classes are made using inheritance, then Windows have controls, so I have a cBaseControl class and controls like buttons and text boxes are made using inheritance.
If the user clicks clicks the mouse the GUI Manager gets a call to it's MouseDown function. The GuiManager Will then work out which window, if any, the mouse down event was for. It will then make a call to that window's MouseDown function.
Now the window will use the MouseDown function it inherited from it's parent cBaseWindow. This function works out if any of the window's controls have been pressed. If a control has been pressed a call is made to the controls MouseDown function (the cBase control class has an empty function) so the call is for the derived class.
Here's where the problem is..
Some times the program works as intended and the call is made to the MouseDown function of the derived control class, however sometimes this is not the case and the call gets sent to the cBaseControl's MouseDown method.
I understood virtual funtions like this:
The type of the object should be used to determine which function should be called incase inherited classes overide that function. Otherwise, the type of the pointer will always be used.
A window has a vector of cBaseControl pointers. When a window is created all the controls are added to the vector. This is done by creating objects of derived control classes with "new" and then using push_back to add to the vector.
The strangest thing is that the problem is intermittant. I can compile the .exe and run it a few times and get different results, sometimes I find that the base method gets called and sometimes i find the dervived class's method is used.
I need some help with this, I am so confused, I have been programming for about a year, and am hoping that this is some kind of newbie problem that can be fixed relatively easily. I have uploaded my project to make things clearer, I would really appriciate any help or feedback.
Try running the exe and clicking a text box field, it might work, it might not, then run it a few more times, and I sure that eventually it will make the wrong call..
Another stange thing is that the top text box of this window always seems too work.. 0_o
Anyhow, please help me, thanks for reading this if you go this far