Don't I understand virtual functions?
-
Hi, I can't figure out the cause of the following: I've derived a general dialog class (say CMyDialogBase) from CDialog and in that class I've put OnOK() with ClassWizard. Then I've derived another dialog classes (say CMyDialogFinal from CMyDialogBase and put in OnOK() as well. Now when I call OnOK() from within CMyDialogFinal::OnOK() I expect CMyDialogBase::OnOK() to be called, but instead CDialog:OnOK() is called, even when I write the call like this CMyDialogBase::OnOK(). With OnCancel I don't get this problem - skipping the base class. Can anyone tell me what is going on here? Thx, Joep
-
Hi, I can't figure out the cause of the following: I've derived a general dialog class (say CMyDialogBase) from CDialog and in that class I've put OnOK() with ClassWizard. Then I've derived another dialog classes (say CMyDialogFinal from CMyDialogBase and put in OnOK() as well. Now when I call OnOK() from within CMyDialogFinal::OnOK() I expect CMyDialogBase::OnOK() to be called, but instead CDialog:OnOK() is called, even when I write the call like this CMyDialogBase::OnOK(). With OnCancel I don't get this problem - skipping the base class. Can anyone tell me what is going on here? Thx, Joep
You have inherit two classes from CDialog: CDialog virtual OnOK() | v CMyDialogBase virtual OnOK() | v CMyDialogFinal virtual OnOK() and implemented in all classes OnOK(). If you will call always CMyDialogBase::OnOK from class MyDialogFinal, then delete function MyDialogFinal::OnOK(). The system will always find the virtual-function in CMyDialogbase and execute this function. If you use virtual-function, the system checks all class (start at class from where you call the function) until it finds the function in this class or a higher-class. I hope that help's.
-
Hi, I can't figure out the cause of the following: I've derived a general dialog class (say CMyDialogBase) from CDialog and in that class I've put OnOK() with ClassWizard. Then I've derived another dialog classes (say CMyDialogFinal from CMyDialogBase and put in OnOK() as well. Now when I call OnOK() from within CMyDialogFinal::OnOK() I expect CMyDialogBase::OnOK() to be called, but instead CDialog:OnOK() is called, even when I write the call like this CMyDialogBase::OnOK(). With OnCancel I don't get this problem - skipping the base class. Can anyone tell me what is going on here? Thx, Joep
Are you certain that you have capitalized OnOK the same in all classes? The fact that CDialog::OnOK is being called when you call CMyDialogBase::OnOK would seem to be a direct indicator that CMyDialogBase::OnOK doesn't exist (or may be spelled or capitalized differently).