一○○分请教一问题

aaa198416aaaa 2006-05-31 11:38:24
#在宏里使用,这个符号是什么意思,有什么用?



CRITICAL_SECTION g_cs; //阻塞模式
bool g_bThreadSafe = true;

inline DXUTLock() { if( g_bThreadSafe ) EnterCriticalSection( &g_cs ); }

#define SET_ACCESSOR( x, y ) inline void Set##y( x t ) { DXUTLock l; m_state.m_##y = t; };

这个宏是什么意思?##起什么作用呢?
...全文
102 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hslinux 2006-05-31
  • 打赏
  • 举报
回复
Token-Pasting Operator (##)
The double-number-sign or “token-pasting” operator (##), which is sometimes called the “merging” operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition.

If a formal parameter in a macro definition is preceded or followed by the token-pasting operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement.

Then, each occurrence of the token-pasting operator in token-string is removed, and the tokens preceding and following it are concatenated. The resulting token must be a valid token. If it is, the token is scanned for possible replacement if it represents a macro name. The identifier represents the name by which the concatenated tokens will be known in the program before replacement. Each token represents a token defined elsewhere, either within the program or on the compiler command line. White space preceding or following the operator is optional.

This example illustrates use of both the stringizing and token-pasting operators in specifying program output:

#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 );

乔小布 2006-05-31
  • 打赏
  • 举报
回复
JF
吃狼的豆腐 2006-05-31
  • 打赏
  • 举报
回复
#,##
# 和 ## 操作符是和#define宏使用的. 使用# 使在#后的首个参数返回为一个带引号的字符串. 例如, 命令

#define to_string( s ) # s

将会使编译器把以下命令

cout << to_string( Hello World! ) << endl;

理解为

cout << "Hello World!" << endl;

使用##连结##前后的内容. 例如, 命令

#define concatenate( x, y ) x ## y
...
int xy = 10;
...

将会使编译器把

cout << concatenate( x, y ) << endl;

解释为

cout << xy << endl;

理所当然,将会在标准输出处显示'10'.
吃狼的豆腐 2006-05-31
  • 打赏
  • 举报
回复
##连接
#替换
jinjiajie 2006-05-31
  • 打赏
  • 举报
回复
##是表示连接!
ymx0330 2006-05-31
  • 打赏
  • 举报
回复
##是表示连接!
LZ慢慢品啊~~~
Z_X_H 2006-05-31
  • 打赏
  • 举报
回复
##表示连接.如:
#define Print(x) Print_##x
void Print_Int()
{
cout << "Print_Int()" << endl;
}
int main()
{
Print(Int)(); // 相当于调用函数Print_Int();
return 0;
}

输出结果: Print_Int
zez 2006-05-31
  • 打赏
  • 举报
回复
连接字符串!!!
#define SET_ACCESSOR( x, y ) inline void Set##y( x t ) { DXUTLock l; m_state.m_##y = t; };

SET_ACCESSOR(1, 2 )
inline void Set2( 1 t ) { DXUTLock l; m_state.m_2 = t; };
ouyh12345 2006-05-31
  • 打赏
  • 举报
回复
##是替换
铖邑 2006-05-31
  • 打赏
  • 举报
回复
是连接的意思。

#define SET_ACCESSOR( x, y ) inline void Set##y( x t ) { DXUTLock l; m_state.m_##y = t; };

SET_ACCESSOR( a, b )
相当于下面:
inline void Setb( a t ) { DXUTLock l; m_state.m_b = t; };

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧