Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Virtual or afx_msg ?

254 views
Skip to first unread message

Eric Hurtebis

unread,
Sep 28, 2000, 1:36:49 AM9/28/00
to
Hello,

I never know which one of the keywords "virtual" or "afx_msg" I should
add in the declaration of a fucntion I manually add.
The compiler never complains.

E.g. I derived a class from CEdit, and I (re)defined the following
functions:
afx_msg UINT OnGetDlgCode();
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
virtual BOOL CDlgGroupEdit::PreTranslateMessage(MSG* pMsg);

Is it correct ? why ?

Thanks,
Eric.


Sent via Deja.com http://www.deja.com/
Before you buy.

Robert Quirk

unread,
Sep 28, 2000, 3:00:00 AM9/28/00
to
How can you program so blindly ?

You declare a message afx_msg if it appears in your message map. The message
map is in effect an array of afx_msg structures.
Virtual is described in any C++ programming book. If a method is described
as virtual then you can override the message in a derived class. That is a
virtual function can be viewed as an override.

Eric Hurtebis <Eric.H...@ign.fr> wrote in message
news:8quldh$b4g$1...@nnrp1.deja.com...

Eric Hurtebis

unread,
Sep 28, 2000, 3:00:00 AM9/28/00
to
In article <8qv15l$o39$1...@supernews.com>,

"Robert Quirk" <rqu...@nildram.co.uk> wrote:
> How can you program so blindly ?
>
> You declare a message afx_msg if it appears in your message map. The message
> map is in effect an array of afx_msg structures.
> Virtual is described in any C++ programming book. If a method is described
> as virtual then you can override the message in a derived class. That is a
> virtual function can be viewed as an override.
>
> Eric Hurtebis <Eric.H...@ign.fr> wrote in message
> news:8quldh$b4g$1...@nnrp1.deja.com...
> > Hello,
> >
> > I never know which one of the keywords "virtual" or "afx_msg" I should
> > add in the declaration of a fucntion I manually add.
> > The compiler never complains.
> >
> > E.g. I derived a class from CEdit, and I (re)defined the following
> > functions:
> > afx_msg UINT OnGetDlgCode();
> > afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
> > virtual BOOL CDlgGroupEdit::PreTranslateMessage(MSG* pMsg);
> >
> > Is it correct ? why ?
> >
> > Thanks,
> > Eric.

Thanks for your explanations (not quite clear for me ;-),

I still have 2 questions:

- a function can be both afx_msg and virtual ? In my examples above, my 3
functions should be declared "virtual" (because OnKeyDown is a overridable
function, that I override for my derived class, isn't it ?)

I declared PreTranslateMessage() as virtual, is it correct ? (Also the
compiler doesn't help me in never complaining whether I write virtual or
afx_msg... so I may take bad habits)

Sorry for my newbie's questions but I'd like to understand...

Joseph M. Newcomer

unread,
Sep 28, 2000, 3:00:00 AM9/28/00
to
Virtual methods have the characteristic that if a virtual method is
called from a class higher in the inheritance hierarchy, the
lowest-level method is called. For example, if CDialog::OnOK is
virtual (as it is), then you would declare CMyDialog::OnOK as virtual,
and when the higher-level logic sees an IDOK message it calls the
virtual method OnOK, which actually transfers control to your
CMyDialog::OnOK because that is the most-derived virtual method.

afx_msg is a macro
#define afx_msg
which expands to exactly nothing. It has no meaning whatsoever in the
C++ language (it isn't a keyword, for example). It is used by the
ClassWizard to indicate a message-handling method (if you omit it,
you'll end up confusing ClassWizard). It is not needed outside the
//{{AFX_ comments, although it is common to use it when defining
handlers not part of the ClassWizard-administered handlers.
joe

On Thu, 28 Sep 2000 11:22:30 GMT, Eric Hurtebis <Eric.H...@ign.fr>
wrote:

>In article <8qv15l$o39$1...@supernews.com>,
> "Robert Quirk" <rqu...@nildram.co.uk> wrote:
>> How can you program so blindly ?
>>
>> You declare a message afx_msg if it appears in your message map. The message
>> map is in effect an array of afx_msg structures.
>> Virtual is described in any C++ programming book. If a method is described
>> as virtual then you can override the message in a derived class. That is a
>> virtual function can be viewed as an override.
>>
>> Eric Hurtebis <Eric.H...@ign.fr> wrote in message
>> news:8quldh$b4g$1...@nnrp1.deja.com...

>> > Hello,
>> >
>> > I never know which one of the keywords "virtual" or "afx_msg" I should
>> > add in the declaration of a fucntion I manually add.
>> > The compiler never complains.
>> >
>> > E.g. I derived a class from CEdit, and I (re)defined the following
>> > functions:
>> > afx_msg UINT OnGetDlgCode();
>> > afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
>> > virtual BOOL CDlgGroupEdit::PreTranslateMessage(MSG* pMsg);
>> >
>> > Is it correct ? why ?
>> >
>> > Thanks,
>> > Eric.
>

>Thanks for your explanations (not quite clear for me ;-),
>
>I still have 2 questions:
>
>- a function can be both afx_msg and virtual ? In my examples above, my 3
>functions should be declared "virtual" (because OnKeyDown is a overridable
>function, that I override for my derived class, isn't it ?)
>
>I declared PreTranslateMessage() as virtual, is it correct ? (Also the
>compiler doesn't help me in never complaining whether I write virtual or
>afx_msg... so I may take bad habits)
>
>Sorry for my newbie's questions but I'd like to understand...
>

>Eric.
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

Doug Harrison [MVP]

unread,
Sep 28, 2000, 3:00:00 AM9/28/00
to

Eric Hurtebis wrote:

>Hello,
>
>I never know which one of the keywords "virtual" or "afx_msg" I should
>add in the declaration of a fucntion I manually add.
>The compiler never complains.
>
>E.g. I derived a class from CEdit, and I (re)defined the following
>functions:
>afx_msg UINT OnGetDlgCode();
>afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
>virtual BOOL CDlgGroupEdit::PreTranslateMessage(MSG* pMsg);
>
>Is it correct ? why ?

afx_msg is currently #defined to nothing. It's used to declare handler
functions for message maps, and handler functions are normally not
virtual. Their polymorphism is achieved by doing a runtime search of
message maps upon receipt of messages. This has interesting
implications for calling a handler from a base class; if you say
OnWhatever(), you will only get the version defined in that class or
one of its bases. To get a derived version, you have to use
SendMessage(). Exceptions to this include OnOK() and OnCancel(), which
are true virtual functions.

PreTranslateMessage is a true virtual function and doesn't participate
in message mapping.

So, use afx_msg only to declare handler functions for message maps,
even though it's currently a no-op as far as the compiler is
concerned. That said, ClassWizard and other extra-language tools may
treat it specially.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows

Scott McPhillips

unread,
Sep 28, 2000, 3:00:00 AM9/28/00
to
Eric Hurtebis wrote:
> The compiler never complains.

> afx_msg UINT OnGetDlgCode();
> afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
> virtual BOOL CDlgGroupEdit::PreTranslateMessage(MSG* pMsg);

These declarations actually do nothing. That's why the compiler never
complains. The afx_msg macro is a wizard mark with no effect on compilation.
The virtual does nothing here *unless* you subsequently derive a class from the
present one. It is common to put the virtual here as a documentation reminder
that you are overriding the parent function, but it is the parent function's
"virtual" declaration that makes the override work. So for the most part you're
really looking at two kinds of comments here.

--
Scott McPhillips [VC++ MVP]

0 new messages