C++ 如何在头文件中定义 const static std::string?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17983511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you define const static std::string in header file?
提问by user1854496
I have a class that I would like to store a static std::string
that is either truly const or effectively const via a getter.
我有一个类,我想std::string
通过 getter存储一个真正 const 或有效 const的静态。
I've tried a couple direct approaches
1.
我尝试了几种直接方法
1。
const static std::string foo = "bar";
2.
2.
const extern std::string foo; //defined at the bottom of the header like so
...//remaining code in header
}; //close header class declaration
std::string MyClass::foo = "bar"
/#endif // MYCLASS_H
I've also tried
我也试过
3.
3.
protected:
static std::string foo;
public:
static std::string getFoo() { return foo; }
These approaches fail for these reasons respectively:
这些方法分别由于以下原因而失败:
- error: in-class initialization of static data member
const string MyClass::foo
of non-literal type - storage class specified for
foo
-- it doesn't seem to like combiningextern
withconst
orstatic
- many 'undefined reference to' errors generated by other parts of my code and even the return line of the getter function
- 错误:
const string MyClass::foo
非文字类型的静态数据成员的类内初始化 - 指定的存储类
foo
- 它似乎不喜欢extern
与const
或static
- 由我的代码的其他部分甚至 getter 函数的返回行生成的许多“未定义引用”错误
The reasonI would like to have the declaration within the header rather than source file. This is a class that will be extended and all its other functions are pure virtual so I currently have no other reason than these variables to have a source file.
我想在头文件而不是源文件中声明的原因。这是一个将被扩展的类,它的所有其他函数都是纯虚拟的,所以除了这些变量之外,我目前没有其他理由拥有源文件。
So how can this be done?
那么如何做到这一点呢?
回答by Chris Cooper
One method would be to define a method that has a static variable inside of it.
一种方法是定义一个内部有静态变量的方法。
For example:
例如:
class YourClass
{
public:
// Other stuff...
const std::string& GetString()
{
// Initialize the static variable
static std::string foo("bar");
return foo;
}
// Other stuff...
};
This would only initialize the static string once and each call to the function will return a constant reference to the variable. Useful for your purpose.
这只会初始化静态字符串一次,每次调用该函数都将返回对该变量的常量引用。对您的目的有用。
回答by Yu Hao
You can only initialize a static const value in the constructor for integer types, not other types.
您只能在构造函数中为整数类型初始化静态常量值,而不能为其他类型初始化。
Put the declaration in the header:
将声明放在标题中:
const static std::string foo;
And put the definition in a .cpp file.
并将定义放在 .cpp 文件中。
const std::string classname::foo = "bar";
If the initialization is in the header file then each file that includes header file will have a definition of the static member. There will be linker errors as the code to initialize the variable will be defined in multiple .cpp files.
如果初始化在头文件中,那么每个包含头文件的文件都会有一个静态成员的定义。将出现链接器错误,因为初始化变量的代码将在多个 .cpp 文件中定义。