C++字符串常量定义方式选择

来源:互联网 发布:pandas 数据分析 实例 编辑:程序博客网 时间:2024/06/03 22:13

#define HELLO "Hello World" 
const char *HELLO2 = "Howdy";

 

What do you prefer? If possible show some drawbacks of eithermethod.

There's one more (at least) road to Rome:

static const char HELLO3[] = "Howdy";

(static —optional — is to prevent it from conflicting with other files). I'dprefer this one over constchar*, because then you'll be able touse sizeof(HELLO3) andtherefore you don't have to postpone till runtime what you can doat compile time.

The define has an advantage of compile-time concatenation, though(think HELLO", World!") and you can sizeof(HELLO) aswell.

But then you can also prefer constchar* and use it across multiple files,which would save you a morsel of memory.

In short — it depends.


0 0