lundi 29 décembre 2014

How to declare non-initialized static std::array of self member, with private constructor?

Sorry if title is confusing, I couldn't find an easy way to write it in a simple sentence. Anyways, the issue I'm facing:



// header:
class SomeThing
{
private:
SomeThing() {} // <- so users of this class can't come up
// with non-initialized instances, but
// but the implementation can.
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];
};


The getThatThing function is meant to return an instance from a lookup table.



// 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


I just can't use a std::array of Something, unless I add a public ctor SomeThing() in the class. It works fine with old-style arrays, I can define the array, and fill it up in the SomeThing::generateLookupTables() function. Apparently the type std::array<SomeThing, 64> does not have a constructor. Any ideas on how to make it work, or maybe a better structure for this concept?


Aucun commentaire:

Enregistrer un commentaire