对不起,如果标题令人困惑,我找不到一个简单的方法来用一个简单的句子来写它。无论如何,我面临的问题:
// header:
class SomeThing
{
private:
SomeThing() {} // <- so users of this class can't come up
// with non-initialized instances, but
// but the implementation can.
int some_data; // <- a few bytes of memory, the default
// constructor SomeThing() doesn't initialize it
public:
SomeThing(blablabla ctor arguments);
static SomeThing getThatThing(blablabla arguments);
static void generateLookupTables();
private:
// declarations of lookup tables
static std::array<SomeThing, 64> lookup_table_0;
static SomeThing lookup_table_1[64];
};
该getThatThing
函数旨在从查找表中返回一个实例。
// in the implementation file - definitions of lookup tables
std::array<SomeThing, 64> SomeThing::lookup_table_0; // error
SomeThing Something::lookup_table_1[64]; // <- works fine
我不能使用std::array
of Something
,除非我在课堂上添加一个公共 ctor SomeThing()
。它适用于旧式数组,我可以定义数组,并在SomeThing::generateLookupTables()
函数中填充它。显然该类型std::array<SomeThing, 64>
没有构造函数。关于如何使其工作的任何想法,或者这个概念的更好结构?
============= 编辑 =======
这种friend std::array<SomeThing, 64>
方法似乎是个好主意,但是:
它也将用于其他地方的数组中。我想保证这个类总是对外部用户保持某些不变量。使用这个友好的数组,用户可能会意外地创建一个未初始化的SomeThing
.
此外,查找表是使用相当复杂的过程生成的,不能按内联方式完成,如std::array<SomeThing, 64> SomeThing::lookup_table_0(some value)