简体   繁体   English

定义变量与模板

[英]Define variables vs. template

In a project, we are creating files to be able to switch languages, during run-time, between English and French.在一个项目中,我们正在创建能够在运行时在英语和法语之间切换语言的文件。

What are the pros and cons to using defines versus template of constant variables.使用常量变量的定义与模板的优缺点是什么。

#define TRAINABLE_LABEL "Trainable:"
#define TRAINABLE_ONE "Easily trainable"
#define TRAINABLE_TWO "Moderately trainable"
#define TRAINABLE_THREE "Highly trainable"

In template:在模板中:

const QString TRAINABLE_LABEL = "Trainable:"
const QString TRAINABLE_ONE = "Easily trainable";
const QString TRAINABLE_TWO = "Moderately trainable";
const QString TRAINABLE_THREE = "Highly trainable";

First of all: why do you call it templates?首先:你为什么称它为模板?

With aa simple #define you are not really protected against redefition of a certain macro, typeness, etc. So macros for these things should be avoided.使用简单的#define您并不能真正防止重新定义某个宏、类型等。因此应避免使用这些宏。

Best way would probably be to use constexpr char* .最好的方法可能是使用constexpr char* This gives you the same behaviour as a macro and will issue an error when some variable is redefined.这为您提供了与宏相同的行为,并且会在重新定义某些变量时发出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM