Re: Homework question relating to docview
On Fri, 13 Jul 2007 13:44:37 GMT, "David Ching" <dc@remove-this.dcsoft.com>
wrote:
Well, I never thought of this as a problem because e.g. if I handle
WM_ERASEBACKGND, in that handler, I've always been able to call
CBaseClass::OnEraseBkgnd() and did not have to resort to SendMessage(). I
don't exactly know what magic makes that work.
When you say CBaseClass::OnEraseBkgnd(), you are defeating the virtual call
mechanism, so "getting polymorphic behavior" doesn't apply. Indeed, you
don't want it when you call the base class version from the derived class,
and this is about the only legitimate scenario in which you can call a
normal, non-virtual message handler directly, by name. Now suppose you have
a normal, non-virtual message handler called "OnWhatever". It would be a
serious mistake to call OnWhatever directly, by name, from some other
message handler "OnSomethingElse", because:
1. It won't be a polymorphic call, so a message map override in a derived
class will not be called.
2. If OnWhatever calls Default(), MFC's "current message" MSG struct is
still set for OnSomethingElse, so it won't do the wrong thing.
Both these problems are solved by using SendMessage/PostMessage, which go
through the message map. Unfortunately, MFC names some virtual functions
such as OnSaveDocument, OnOK, OnInitDialog, etc similarly to ordinary
non-virtual message handlers, so there is great potential for confusion.
..
--
Doug Harrison
Visual C++ MVP