Re: Homework question relating to docview
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:en0f931633cj42s0gqm5po3egv07eb8b3p@4ax.com...
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.
Why is OnEraseBkgnd special?
I don't know what you mean. When my handler merely adds to the behavior of
the base one, I also want the base's behavior. There's nothing about
"defeating the virtual call mechanism" about that. The virtual call
mechanism merely means my class gets first crack at handling it, not
dictating what I can and can't do once it is called.
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.
Of course, we were not talking about calling OnWhatever from
OnSomethingElse. We were talking about calling it from OnMyWhatever (the
handler in the derived class).
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.
Indeed. I once made the mistake of trying to use __super to reference the
base class, only to find it did not work correctly from a message handler
that I had confused with a virtual function due to both functions starting
with "On".
-- David