简体   繁体   English

在类定义中声明了C ++宏?

[英]C++ macro declared in a class definition?

I've spent the past couple of days learning C++, but I just came across something that I hadn't seen in the books nor after doing some research on Google. 我花了几天的时间学习C ++,但是我遇到的东西是我在书本上以及在Google上进行一些研究之后都没有看到的。

As far as I know, a macro is a statement or "variable" name that is preceded by #define hat allows certain values or functions to be specified later and inserted where desired automatically. 据我所知,宏是在#define hat之前的语句或“变量”名称,它允许稍后指定某些值或函数,并在需要的位置自动插入。

However, I've come across a function that is declared inside a clas and not preceded by #define and it is called a "macro". 但是,我遇到了一个在clas内声明的函数,而不是在#define之前,它被称为“宏”。 The function is from MFC and is called DECLARE_MESSAGE_MAP . 该函数来自MFC,称为DECLARE_MESSAGE_MAP http://msdn.microsoft.com/en-us/library/08ea0k43.aspx http://msdn.microsoft.com/zh-CN/library/08ea0k43.aspx

Can someone explain what this type of macro is; 有人可以解释这种宏是什么吗? what is it called (so I could further research it) and what does it mean? 它叫什么(以便我进一步研究),它是什么意思?

DECLARE_MESSAGE_MAP is just a #define that is defined in the MFC (Afx.h?) set of includes. DECLARE_MESSAGE_MAP只是在MFC(Afx.h?)包含项集中定义的#define。 there is nothing special about compared to any other #define. 与任何其他#define相比,没有什么特别的。

This is an old book : MFC Internals but it's a classic if you want to learn what all those things in the MFC actually do and how they work. 这是一本古老的书:《 MFC Internals》,但是如果您想了解MFC中所有这些功能实际上是如何工作以及它们是如何工作的,则它是经典著作。

You may find this interesting:- 您可能会发现很有趣:

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. 如果您决定清理并重新排列代码,则最好注意MFC派生类头文件中将出现的DECLARE_MESSAGE_MAP()宏。 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. 解决方案是将其保留在受保护的存储类中。 ie, 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. 在正确声明存储类的情况下,使公共函数和变量位于保护部分的上方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM