## # C++

# 和 ## 在宏定义(define)中经常可以看到,是预编译过程中的常用语句,本文记录了本人探索 # 和 ## 区别以及使用的内容

先简单的将这两个符号进行标签化,然后再记录具体实验。

#   -- 转换, 完成代码到字符串的转换

## -- 连接, 完成代码的连接

示例:

1. # 转换代码为字符串

#define CONVERT(name) #name
 
int main(int argc, char* argv[])
{
    printf("You and %s are friends.\n", CONVERT(James));
    return 0;
}
    这里, James 将在预编译过程中被转换为字符串 "James" 。

2. ## 代码连接

#define CAT(batman, robin) batman ## robin
 
#define make_friend(index)  printf("You and %s are friends.\n", CAT(james, index));
 
int main(int argc, char* argv[])
{
    char* james001="fake James 001";
    char* james007="James Bond";
    char* james110="fake James 110";
 
    make_friend(001);
    make_friend(007);
    make_friend(110);
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/Gussss/article/details/89563587
C++