请教一个C++中关于符号"#"和"##"的问题

lemonal 2007-04-24 02:10:07
这是一个宏定义:

#define P( className ) \
int className ## create{ \
...... //内容省略 \
const char* className::para1 = #className "create"; \
...... //内容省略 \
}

我想问的是,这段中的"##"和"#"这两个符号分别代表什么意思。
烦请指点,多谢了!
...全文
298 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
followme163 2007-04-24
  • 打赏
  • 举报
回复
在MFC中使用#是为了防止类名重复而已,看看精通MFC就好了
Caten 2007-04-24
  • 打赏
  • 举报
回复
#直接用来替换为字符串
##可以进行连接(通常连接变量名,MFC里用得很多)

#define paster( n ) printf( "token" #n " = %d", token ##n )

int token8 = 8;
int token9 = 9;

paster( 8 );
paster( 9 );
就替换为:
printf( "token8 " = %d", token8 );
printf( "token9 " = %d", token9 );

这种方法在特定场合有特效,并且用别得方式实现起来基本很困难,LINUX及MFC中有很多
followme163 2007-04-24
  • 打赏
  • 举报
回复
##是连接字符串
#是将宏定义中的变量转换为字符串
例如
#define P(A) #A
#define Q(A,B) A##B
源码:
int main()
{
cout<<P(a)<<endl;
cout<<Q("a","b")<<endl;
}
编译后为:
int main()
{
cout<<"a"<<endl;
cout<<"a""b"<<endl;
}
输出
a
ab
newRoadExplorer 2007-04-24
  • 打赏
  • 举报
回复

给个例子你就明白了:

#include <iostream>
using namespace std;

#define P( para ) \
char * mychar = #para "123"\
P(hello);
int main()
{
printf("mychar = %s\n", mychar);
}

输出的结果就是:mychar = hello123
taodm 2007-04-24
  • 打赏
  • 举报
回复
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 );

lemonal 2007-04-24
  • 打赏
  • 举报
回复
如果您知道的话
烦请指点一下
谢谢
taodm 2007-04-24
  • 打赏
  • 举报
回复
随便一本合格的C语言书,或者msdn都能给你答案

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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