5

C++中的#和##运算符

 3 years ago
source link: http://yuanfentiank789.github.io/2018/11/01/C++-%E5%92%8C/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

#和##运算符

#:构串操作符

构串操作符#只能修饰带参数的宏的形参,它将实参的字符序列(而不是实参代表的值)转换成字符串常量

#define STRING(x)  #x#x#x
#define TEXT(x)  "class"#x"Info"
//宏引用:
int abc = 100;
STRING(abc);
TEXT(abc);
//替换结果:
abcabcabc
classabcInfo

##:合并操作符

合并操作符##将出现在其左右的字符序列合并成一个新的标识符

使用合并操作符##时,自身的标识符必须预先有定义,否则编译器会报“标识符未定义”的编译错误。 字符序列合并成新的标识符不是字符串。

#define CLASS_NAME(name) class##name
#define MERGE(x,y) x##y##x
//宏引用:
CLASS_NAME(Timer)
MERGE(me,To)
//替换结果:
classTimer
meTome



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK