老是有一些问题不懂! 用法 ## 不解请教高人!
这下面的 ## 表示什么? 为什么这么用? 我不明白请指教!
#define BIT_MASK(__bf) (((1U << (bw ## __bf)) - 1) << (bs ## __bf))
问题点数:20、回复次数:15Top
1 楼lyskyly(浮生三笑)回复于 2006-05-02 09:37:17 得分 4
表示将bw与__bf连接起来
比如
#define MAKE(m) cout<<n##m;
int nm = 0;
MAKE(m)
此时输出0Top
2 楼feto(酒肉.程序员)回复于 2006-05-02 09:54:23 得分 2
#define MAKE(1) cout<<n##m
被替代为
cout<<n1Top
3 楼langzi8818(┤天道酬勤┝爱老婆┦┷我是来学习滴┷)回复于 2006-05-02 12:29:27 得分 1
连接符Top
4 楼sankt(宠辱不惊,看庭前花开花落;去留无意,望天空云卷云舒.)回复于 2006-05-02 12:42:12 得分 1
Here is an example of the ## command:
#define concatenate( x, y ) x ## y
...
int xy = 10;
...
This code will make the compiler turn
cout << concatenate( x, y ) << endl;
into
cout << xy << endl;
which will, of course, display '10' to standard output.
Top
5 楼chenhu_doc(^0^纯一狼^0^ 看书看到大笑,直到不能自已)回复于 2006-05-02 14:04:41 得分 0
agree....Top
6 楼chenhu_doc(^0^纯一狼^0^ 看书看到大笑,直到不能自已)回复于 2006-05-02 14:12:41 得分 2
#define concatenate( x, y ) x ## y
after a try, i found that it equals :
#define concatenate( x, y ) xy
that is to say : ## contact two characters together and consider them to one variable in program...
so :
#define concatenate( x, y ) xyz
//equals :
#define concatenate( x, y ) x ## y ## z
and the file of program is as below:
#define concatenate( x, y, z ) x ## y ## z
#include <iostream.h>
// int xy = 10;
// ...
//This code will make the compiler turn
void main()
{
int xyz = 10;
cout << concatenate( x, y, z ) << endl;
}
Top
7 楼avalonBBS("︶.︶メ)→( ̄ε ̄メ)回复于 2006-05-02 15:50:21 得分 0
连接Top
8 楼avalonBBS("︶.︶メ)→( ̄ε ̄メ)回复于 2006-05-02 15:50:48 得分 2
在编译前的预编译阶段连接的Top
9 楼atgjplh(永远的C/C++(unix/liunx))回复于 2006-05-02 22:11:11 得分 0
知道这回事了! 但还不知道怎么来用! 或着说不知道什么情况下用比较好!Top
10 楼chenhu_doc(^0^纯一狼^0^ 看书看到大笑,直到不能自已)回复于 2006-05-02 22:13:00 得分 1
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 );
//////////
copy from msdn......Top
11 楼DelphiNew(沙鼠)回复于 2006-05-02 22:15:41 得分 2
MFC里面用##连接很多,主要是用以提供RuntimeClass,消息映射,动态加载,类序列化
之类的东西,以后你慢慢就会都明白的。。。
多看些深入的C++ 和 MFC的书吧。。Top
12 楼all4u(东方欲晓)回复于 2006-05-03 21:00:58 得分 2
呵呵,同意楼上的。
好象《深入浅出MFC》开头里就说到这个了,偶也是在那里才知道的。Top
13 楼rickerliang(专回0分贴(来交流不是为分))回复于 2006-05-03 21:13:20 得分 0
##就是纯文本文字连接Top
14 楼atgjplh(永远的C/C++(unix/liunx))回复于 2006-05-08 20:24:58 得分 0
就像这个一样是什么个用法
#define CObj(className) Create##className()Top
15 楼CodeFly()回复于 2006-05-25 21:07:27 得分 3
C语言预定义中比较常见符号有:(1)## (2)#@ (3)# ,3种用法如下:
#define A(x) T_##x
举例:此时 A(1) 会被翻译成 T_1
#define B(x) #@x
举例:此时 B(1) 会被翻译成 '1'
#define C(x) #x
举例:此时 C(1) 会被翻译成 "1"Top




