NH> I have a question about inheritance and the flow of logic. There is
NH> a class, CCommUI, which is decended from CDialog. CComUI contains a
NH> method, OnCancel, which is included in the appropriate message map to
NH> be called whenever the "Cancel" button in the modal dialog box (the
NH> CComUI object) is pressed.
CDialog::OnCancel() is a virtual function. It's implementation is really
simple, it just calls CDialog::EndDialog() passing IDCANCEL.
NH> void CComUI::OnCancel()
NH> {
NH> CWnd * pbutton = GetDlgItem(IDCANCEL);
NH> pbutton->EnableWindow(FALSE);
NH> .
NH> .
NH> .
NH> CDialog::OnCancel();
NH> }
NH> The question I have is this:
NH> Does the line containing the call to the parent "percolate" up to the
NH> child?
It works just like any other virtual function. The system knows what
type of object it is that's making the call to the virtual OnCancel()
and calls the appropriate function. If it's an object of type CComUI,
CComUI::OnCancel() is called. If it's an object of type CDialog,
then CDialog::OnCancel() is called. If it's something derived from
CDialog and this something has no override for OnCancel(), then
CDialog::OnCancel() is called. If the object is derived from
CComUI and this class has no OnCancel() override, then CComUI::OnCancel()
is called.
NH> What is the purpose of this call in the first place if
NH> CDialog::OnCancel() had never been specifically fleshed out (or even
NH> mentioned) anywhere in the code?
As mentioned, there is an implementation of CDialog::OnCancel() which
the CComUI class inheirited and then overrode. The original code is
still out there and is ready to call.
This is kinda typical of how MFC is used. To override a function,
you either do your thing and call the base class or else you call
the base class version and do your own thing. (There are, of course,
plenty of exceptions.)
NH> As an aside, I commented this line out and the software ran as
NH> expected. With that line left in the code there was always a
NH> premature closing of the dialog box. Does this mean that the method,
NH> CDialog::OnCancel(), was performing some action on its own without me
NH> even being aware of it?
Yes. There is a message map entry inside the framework that handles
an IDCANCEL message. This message is generated if you press a button
that sends IDCANCEL or if you press in the dialog. This
message map entry points to OnCancel().
---
þ Blue Wave/QWK v2.12 þ
---------------
* Origin: St. Louis Users Group (1:100/4)
|