是否可以将const char*成员初始化为char数组的组合?

Is it possible to initialize a const char* member as a composite of char arrays?

本文关键字:char 数组 组合 初始化 const 是否 成员      更新时间:2023-10-16

这应该很简单:

类中有一个静态const char*成员。我想把它初始化为一个复合字符串。例如:

const char* s_firstPart = "Hello I'm the first part.";
const char* s_secondPart = "I'm the second part.;
struct MyClass
{
  static const char* s_theFinalString;
}
const char* MyClass::s_theFinalString = "Here is the string: " 
    + s_firstPart ++ s_secondPart; // obviously this won't compile

我的问题是:是否有一种方法将const char*成员初始化为复合字符数组?

正确答案来自评论中的@dauphic:

不,你不能。您应该使用std::string。与您想要的类似的东西可以用宏来管理,但不应该这样做。