在宏中连接字符串 - C ++ [英] Concatenating strings in macros - C++

查看:151
本文介绍了在宏中连接字符串 - C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

连接宏中定义的字符串的最简单方法是什么。
ie我正在寻找的伪代码是:

What's the easiest way to concatenate strings defined in macros. i.e. The pseudo code I'm looking for would be like:

#define ROOT_PATH "/home/david/"
#define INPUT_FILE_A ROOT_PATH+"data/inputA.bin"
#define INPUT_FILE_B ROOT_PATH+"data/inputB.bin"
...
#define INPUT_FILE_Z ROOT_PATH+"data/inputZ.bin"

我知道的唯一方法是在代码中使用strcat,类,然后c_str方法,但是当我有很多输入文件时,它可能会弄乱。我想直接使用INPUT_FILE_A等,没有大量的局部变量。是否有很好的方法可以做到这一点?

The only way I know of is to use strcat in the code, or using the string class and then the c_str method, but it can get messy when I have lots of input files. I'd like to just use INPUT_FILE_A, etc. directly and not have lots of local variables. Is there a good way to do this?

谢谢。

推荐答案

>编译器将自动连接相邻字符串:

The compiler will automatically concatenate adjacent strings:

#define ROOT_PATH "/home/david/"
#define INPUT_FILE_A ROOT_PATH "data/inputA.bin"

或更多通用:

#define INPUT_FILE_DETAIL(root,x) root #x
#define INPUT_FILE(x) INPUT_FILE_DETAIL(ROOT_PATH "data/", x)

这篇关于在宏中连接字符串 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆