在C++中,我想定义一些将在类中使用的字符串,但这些值在所有实例中都是通用的.在CI中会使用#defines.这是一个尝试:
#include <string>
class AskBase {
public:
AskBase(){}
private:
static std::string const c_REQ_ROOT = "^Z";
static std::string const c_REQ_PREVIOUS = "^";
static std::string const c_REQ_VERSION = "?v";
static std::string const c_REQ_HELP = "?";
static std::string const c_HELP_MSG = " ? - Help\n ?v - Version\n ^^ - Root\n ^ - Previous\n ^Z - Exit";
};
int main(){AskBase a,b;}
Run Code Online (Sandbox Code Playgroud)
如果需要C++ 0x,那是可以接受的.
cop*_*pro 10
您必须在单个翻译单元(源文件)中单独定义它们,如下所示:
//header
class SomeClass
{
static const std::string someString;
};
//source
const std::string SomeClass::someString = "value";
Run Code Online (Sandbox Code Playgroud)
我相信新的C++ 1x标准将解决这个问题,尽管我并不完全确定.
| 归档时间: |
|
| 查看次数: |
2520 次 |
| 最近记录: |