Be careful with DECLARE_MESSAGE_MAP()


If you decide to clean up and re- arrange your code, better be careful about the DECLARE_MESSAGE_MAP() macro that will be present in an MFC derived class header file. This macro contains a “protected” storage class declaration. So everything that comes under this macro will be protected unless there is any other storage class specified after that. Normal error that occur during compilation will be cannot access the private variable. But you cannot easily figure out what went wrong you can see only a macro call and a public storage class above.

#ifdef _AFXDLL
#define DECLARE_MESSAGE_MAP() \
private: \
    static const AFX_MSGMAP_ENTRY _messageEntries[]; \
protected: \
    static AFX_DATA const AFX_MSGMAP messageMap; \
    static const AFX_MSGMAP* PASCAL _GetBaseMessageMap(); \
    virtual const AFX_MSGMAP* GetMessageMap() const; \

#else
#define DECLARE_MESSAGE_MAP() \
private: \
    static const AFX_MSGMAP_ENTRY _messageEntries[]; \
protected: \
    static AFX_DATA const AFX_MSGMAP messageMap; \
    virtual const AFX_MSGMAP* GetMessageMap() const; \

#endif

The solution is to leave it inside a protected storage class. i.e, declare a protected storage class just above it and declare the functions and variables that require to be protected, below it. Let the public functions and variables be above the protected section with proper declaration of the storage class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s