现在的位置: 首页 > 综合 > 正文

显示C/C++中定义的宏命令文字技巧

2013年10月28日 ⁄ 综合 ⁄ 共 3271字 ⁄ 字号 评论关闭

这种技巧在C/C++编程过程中十分有用!希望对所有GGDDMMJJ有所帮助。下面开始介绍实现方法:

其实很简单,平时我们都爱在自己的CPP代码中填写一些宏定义,例如下面的:

#define  MyMacro_FileType_Error        0x0000001

#define  MyMacro_CloseFile_Error        0x0000002

......

然后在以后的代码中应用这些Macro,非常方便,对不?:D

如果这些定义的是某些错误代码(或者别的啥)的话,我们通常会在屏幕或日志中显示它们代表的含义。阅读这些宏命令时,不难发现它们都定义地十分具有可读性,一般人一眼就能知道它们代表什么意思。如何将这些宏直接显示在日志或屏幕上,以减少我们书写的麻烦呢?

解决方法请看:

#define  ERROR_TEXT_(e)     #e    // <--- 看这里! That's so easy, right? :P

#define  MyMacro_FileType_Error        0x0000001

#define  MyMacro_CloseFile_Error        0x0000002

......

然后,我们可以在代码中这样写道:

        cout << ERROR_TEXT_(MyMacro_FileType_Error) << endl;

执行后,屏幕会直接显示出“MyMacro_FileType_Error”的字样,爽吧:)

你也可以对ERROR_TEXT_(e)这句宏进行改造,让它的实用性更广些!

比如定义成这样:

#define ReturnText_(e)   case e: return #e
#define MyMacro_FileType_Error     0x000001
#define MyMacro_CloseFile_Error   0x000002
#define MSGS() /
        ReturnText_(maHelloWorld1); /
        ReturnText_(maHelloWorld2);

这时你可以定义一个函数用于显示信息文字内容,

string  GetErrorText (int  nErrorCode)

{

switch(nErrorCode)

{

MSGS();

// 这里可以还可以继续添加其他信息处理

} // end switch

}

.....

在代码中直接调用GetErrorText()即可,

cout << GetErrorText (MyMacro_FileType_Error).c_str () << endl;

下面引用一下MSDN中文章:

索引关键字=# (stringizing operator)


Stringizing Operator (#)

The number-sign or “stringizing” operator (#) converts macro parameters (after expansion) to string constants. It is used only with macros that take arguments. If it precedes a formal parameter in the macro definition, the actual argument passed by the macro invocation is enclosed in quotation marks and treated as a string literal. The string literal then replaces each occurrence of a combination of the stringizing operator and formal parameter within the macro definition.

White space preceding the first token of the actual argument and following the last token of the actual argument is ignored. Any white space between the tokens in the actual argument is reduced to a single white space in the resulting string literal. Thus, if a comment occurs between two tokens in the actual argument, it is reduced to a single white space. The resulting string literal is automatically concatenated with any adjacent string literals from which it is separated only by white space.

Further, if a character contained in the argument usually requires an escape sequence when used in a string literal (for example, the quotation mark (") or backslash (/) character), the necessary escape backslash is automatically inserted before the character. The following example shows a macro definition that includes the stringizing operator and a main function that invokes the macro:

#define stringer( x ) printf( #x "/n" )

void main()
{
    stringer( In quotes in the printf function call/n ); 
    stringer( "In quotes when printed to the screen"/n );   
    stringer( "This: /"  prints an escaped double quote" );
}

Such invocations would be expanded during preprocessing, producing the following code:

void main()
{
   printf( "In quotes in the printf function call/n" "/n" );
   printf( "/"In quotes when printed to the screen/"/n" "/n" );
   printf( "/"This: ///" prints an escaped double quote/"" "/n" );
}

When the program is run, screen output for each line is as follows:

In quotes in the printf function call

"In quotes when printed to the screen"

"This: /" prints an escaped double quotation mark"

Microsoft Specific

The Microsoft C (versions 6.0 and earlier) extension to the ANSI C standard that previously expanded macro formal arguments appearing inside string literals and character constants is no longer supported. Code that relied on this extension should be rewritten using the stringizing (#) operator.

END Microsoft Specific


或者可以直接在线访问微软相关网址:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/prepr_13.asp

抱歉!评论已关闭.