先看代码:
#include <iostream>
#define display1(name) std::cout<<#name<<std::endl
#define display2(name) std::cout<<"#name"<<std::endl
#define display3(name) std::cout<<""#name""<<std::endl
void test_command(char* content){std::cout<<content<<std::endl;}
void help_command(char* content){std::cout<<content<<std::endl;};
struct Command {
char *name;
void (*function)(char*);
};
#define COMMAND(NAME) { #NAME, NAME##_command }
int main() {
display1("name1");
display1(name1);
display2("name2");
display2(name2);
display3("name3");
display3(name3);
struct Command commands[] = {COMMAND(test),COMMAND(help)};
for (auto item:commands) {
item.function(item.name);
}
return 0;
}
执行结果:
"name1"
name1
#name
#name
"name3"
name3
test
help
1.#的功能是将其后面的宏参数进行字符串化操作
2.## 被称为连接符