我刚刚发现以下内容是无效的。
//Header File
class test
{
const static char array[] = { '1', '2', '3' };
};
最好的初始化位置在哪里?
我刚刚发现以下内容是无效的。
//Header File
class test
{
const static char array[] = { '1', '2', '3' };
};
最好的初始化位置在哪里?
最佳位置应该在源代码文件中
// Header file
class test
{
const static char array[];
};
// Source file
const char test::array[] = {'1','2','3'};
你可以像你尝试的那样在类声明中初始化整数类型; 所有其他类型必须在类声明外进行初始化,且仅需一次。
class test {
static const char array(int index) {
static const char a[] = {'1','2','3'};
return a[index];
}
};
这种编程范式有几个好处:
&a[1]
的一致性。 - Alexander Oh//Header File
class test
{
const static char array[];
};
// .cpp
const char test::array[] = { '1', '2', '3' };
现在,在C++17中,你可以使用内联变量
一个简单的静态数据成员(N4424):
在你的例子中:
struct WithStaticDataMember { // This is a definition, no outofline definition is required. static inline constexpr const char *kFoo = "foo bar"; };
//Header File class test { inline constexpr static char array[] = { '1', '2', '3' }; };
应该只需要工作
static constexpr
已经包含了 inline
的意思:https://dev59.com/HmYq5IYBdhLWcg3wjBQM#57407675 - Ciro Santilli OurBigBook.com使用 constexpr
,即使在 C++11 中也必须在头文件中定义值
如果您使用 constexpr
而不是 const
,那么这个答案建议您甚至必须在头文件中定义即使在 C++11 中:
#include <cassert>
struct MyClass {
static constexpr int is[] = {1, 2, 3};
static constexpr int i = 1;
};
// TODO is this ever mandatory? Create example that fails on -std=c++11.
// Pretty sure never mandatory in C++17 https://dev59.com/sHI95IYBdhLWcg3w3h_G#40959093
// constexpr int MyClass::is[];
int main (void) {
assert(MyClass::is[0] == 1);
assert(&MyClass::is[0] == &MyClass::is[1] - 1);
assert(MyClass::i == 1);
assert(&MyClass::i == &MyClass::i);
}
编译并运行的方法:
g++-10 -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o main.out main.cpp
./main.out
struct MyClass {
static constexpr int is[];
};
constexpr int MyClass::is[] = {1, 2, 3};
编译失败,错误信息如下:
main.cpp:4:26: error: ‘constexpr’ static data member ‘is’ must have an initializer
在Ubuntu 20.04上测试通过。
namespace {
const char test_init_array[] = {'1', '2', '3'};
}
class test {
public:
const char * const array;
test() : array(test_init_array) {}
};
.h
文件是声明,.c
文件是定义,因此仅在头文件中声明整数类型并将其引用会导致编译器错误:undefined reference to test::SOME_INTEGER
。 (我意识到这听起来非常苛刻和学究,但我并不想故意制造麻烦;我只是想确保我使用正确的术语,所以如果我错了,请务必纠正我)。 - dwanderson