#include <iostream>
using namespace std;
// # 构串操作符
// 构串操作符#只能修饰带参数的 宏的形参,它将实参的字符序列(而不是实参代代表的值)转换成字符串常量
#define MKSTR( x ) #x#x#x
#define TEXT( x ) "class"#x"Info"
// ## 合并操作符
// 合并操作符##将出现在其左右的字符序列合并成一个新的标记符
// 注意:使用合并操作符##时,自身的标记符必须预先有定义,否则编译器会报“标记符未定义”的编译错误
// 字符序列合并成新的标记符并不是字符串
#define CONTACT(x, y) x##y
int main(int argc, char const *argv[])
{
/* code */
int age = 10;
cout<<MKSTR(age)<<endl;
char* name = "abc";
cout<<TEXT(name)<<endl;
int xy = 100;
int ab = 10;
cout<< CONTACT(x, y)<<endl;
cout<< CONTACT(a,b)<<endl;
char* hello = "nice";
char* world = "meet you";
// 未定义标识符 "helloworld"
//cout<< CONTACT(hello, world)<<endl;
char* helloworld = "good job";
// 通过上面的测试可以知道合并好的字符序列(例如 helloworld)必须要先要定义好,否则就会报出未定义标记符的错误
cout<<CONTACT(hello, world)<<endl;
return 0;
}
输出如下:
ageageage
classnameInfo
100
10
good job