首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

原来#define 也可以写成# define的,里面有个看不懂的地方,该如何处理

2012-02-07 
原来#define 也可以写成#define的,里面有个看不懂的地方#defineENTRY(fcn)traceEntry(__FILE__,__LINE__,#

原来#define 也可以写成# define的,里面有个看不懂的地方
#   define   ENTRY(fcn)           traceEntry   (__FILE__,   __LINE__,   #fcn)
                                                                                                              ~~~这里加#是干什么的
谢谢了

[解决办法]
串化 .... 比如 ENTRY(1+2) 被替换成 traceEntry (__FILE__, __LINE__, "1+2 " )
[解决办法]
#x 替换时自动在两边加上引号
##x 替换时自动将内容和周围连接起来,如:
#define A(x,y) x##y
A(o,k) -> ok
[解决办法]
C/C++ Preprocessor Reference

Stringizing Operator (#)
Token-Pasting Operator (##)
Charizing Operator (#@)
[解决办法]
c primier里面讲的比较清楚
[解决办法]
强烈建议你看MSDN。。。

Stringizing Operator (#)

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

int 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:

int 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 "

------------------------------------------

Token-Pasting Operator (##)

#define paster( n ) printf( "token " #n " = %d ", token##n )
int token9 = 9;
If a macro is called with a numeric argument like

paster( 9 );
the macro yields

printf( "token " "9 " " = %d ", token9 );
which becomes

printf( "token9 = %d ", token9 );

------------------------------------------

Charizing Operator (#@)

#define makechar(x) #@x
causes the statement

a = makechar(b);
to be expanded to

a = 'b ';
The single-quotation character cannot be used with the charizing operator.

热点排行