Some special uses in Macro in CC ++

xiaoxiao2021-03-06  106

After ISO C launched, the use of C / C Zhonghong (Macro) has been controversial, just like the GOTO statement that is conservative in the year. Different is that the usage rate of using a goto statement in C / C code is less and less. Macro still occupies a place in C / C programing - such as a combination of brightened and destroyed Microsoft Foudation Classes to play Macro's powerful features. With "there is reasonable", Macro's presence must have its reasonable factors. In fact, if you get the Macro mechanism when Programming, you can write some clever and exquisite and unified code.

I read a engine code in my work, and the engine was smart in somewhere using Macro's functionality. Here I will organize it for you to learn.

Everyone often defines some of the types of enumerated types when performing C / C Programming. For example:

Enum EMESSAGE {msg_open, msg_close, msg_copy, msg_paste};

At some point, for example, when debugging a certain program, use the debugger to capture a value of a certain enumeration variable, and we can get some values. For example, when the variable EMESSAGE EMSG = MSG_Open, we can observe EMSG = 0. But a single "0" thing is that it makes it difficult to understand its meaning. If you want to print an enumerated name on the screen, it is even more difficult.

Solving this problem, one of the methods is to use Macro's powerful and flexible mechanism. Now let's take a step by step to analyze how to use Macro to solve the above problems.

Want to print the name of the enumeration, you first think of it is: use a dictionary data structure to record enumeration variables and map of the string. E.g:

#include

Using namespace std;

Map msgdesc; // In order to save space, not use String Storage Description Information

Msgdesc [msg_open] = "msg_open"; msgdesc [msg_close] = "msg_close"; msgdesc [msg_copy] = "msg_copy"; msgdesc [msg_paste] = "msg_paste";

It is very convenient to use the dictionary data structure. ...). If the enumeration value is much more, you will write once in the enumeration definition every addition of the enumeration.

If you use the Macro mechanism, you can process the above problems by precompiling and the MACRO mechanism.

For example: in the file msgdef.h #ifndef _msgdef_h_ # define _msgdef_h_

#undef usemacro

#if Defined (include_as_enum) // Judgment the setting of the precompiled macro, is it to convert to ENUM or String #define msg_macro (msg) msg, # Elif Defined (include_STRING) #define msg_macro (msg) # msg, // cannot forget "" No. #else #error to use this include file, first define either INCLUDE_AS_ENUM or INCLUDE_AS_STRING # endif // enum values ​​have newly defined message, then you just need to add here MSG_MACRO (MSG_OPEN) MSG_MACRO (MSG_CLOSE) MSG_MACRO ( MSG_copy) MSG_Macro (MSG_Paste)

#ENDIF

When you use, you should use this, for example in main.cpp.

// Define the description of the message array char msgdesc [32] [32] = // to estimate the number of MSGs {#define include_AS_String // To use one of the two macros specified in MSGDef.h, otherwise it will compile errors #include "msgdef.h" #undef incruDe_as_string // To cancel it so that it is set to};

/ / Define the enumeration type of the message enum eMessage {#define include_as_enum #include "msgdef.h" #undef incrude_as_enum};

This allows you to use the enumerated and describe its string information.

In addition, this article comes with a complete sample program, compiled in this unit (Windows 2000 SP4, VC6 SP5), but it seems that 9CBS Blog cannot upload attachments (perhaps, may wish to enlighten it), so it needs example programs Friends, please let me: son_of_lover@163.com.

Welcome to exchange finger.

转载请注明原文地址:https://www.9cbs.com/read-98813.html

New Post(0)