我有课
class foo {
public:
foo();
foo( int );
private:
static const string s;
};
初始化字符串的最佳位置在哪里s
在源文件中?
任何地方在one编译单元(通常是 .cpp 文件)会执行以下操作:
foo.h
class foo {
static const string s; // Can never be initialized here.
static const char* cs; // Same with C strings.
static const int i = 3; // Integral types can be initialized here (*)...
static const int j; // ... OR in cpp.
};
foo.cpp
#include "foo.h"
const string foo::s = "foo string";
const char* foo::cs = "foo C string";
// No definition for i. (*)
const int foo::j = 4;
(*) 根据您必须定义的标准i
在类定义之外(例如j
is) 如果它用在除整型常量表达式之外的代码中。有关详细信息,请参阅下面大卫的评论。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)