I have 3 classes, myForm, myFormTest and myFormManager.
This is a simple version of what I have
1 2 3 4 5 6 7 8 9 10 11 12
class myFormManager {
myForm * formPointers;
void addForm(myForm &theform)
//code to add theform to formPointers
}
int execsomefunction(int formIndex) {
return formPointers[formIndex].somefunction();
}
1 2 3 4 5
class myForm {
int somefunction();
}
1 2 3 4
class myFormTest: public myForm {
int somefunction(); //override
}
So I created some code with an instance of both the myFormTest class and the myFormManager class and add the form using myFormManager.addForm(). when i call execsomefunction() in the manager class it runs but doesnt perform the override.
I realise "slicing" is occurring and I believe I understand WHY it is happening... but I don't know what to change to resolve it. myFormManager knows nothing about the myFormTest class only the myForm class... so how can I make it execute the override???? Ummmm
(I think) my first mistake is that I am adding an instance of myForm rather than a reference to an instance of myForm defined in the main(). I am not quite sure how to do this.
(I think) my second mistake is that the manager only knows about myForm not myFormTest so any reference I make in this class will cause slicing to occur.
You should post all relevant code, i'm just filling in some blanks.
You have an array of myForm, so when you add a new form you are only creating a copy of myForm. All other data regarding the vtable and myFormTest's variables won't be stored. You need an array of pointers to myForm, so you are not actually storing a myForm.
I do need the virtual bit... I was wrong. Googled it...
You have an array of myForm, so when you add a new form you are only creating a copy of myForm. All other data regarding the vtable and myFormTest's variables won't be stored. You need an array of pointers to myForm, so you are not actually storing a myForm.