죄송합니다. 제목이 혼란 스럽다면, 간단한 문장으로 쉽게 작성할 수 없습니다. 어쨌든, 문제는 내가 직면하고있어 :개인 생성자 및 정적 배열 자체가있는 클래스
// 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
기능은 조회 테이블에서 인스턴스를 반환하기위한 것입니다. 나는 수업 시간에 공공의 ctor의 SomeThing()
을 추가하지 않으면
// 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
Something
의를 사용할 수 없습니다. 예전 스타일의 배열에서도 잘 작동하며, 배열을 정의하고 SomeThing::generateLookupTables()
함수로 채울 수 있습니다. 외관상으로는 std::array<SomeThing, 64>
유형에는 생성자가 없습니다. 그것을 작동시키는 방법에 대한 아이디어, 아니면이 개념을위한 더 나은 구조?
============= 편집 =======
friend std::array<SomeThing, 64>
접근 방식은 좋은 생각처럼 보이지만 :
사용하는 것입니다 다른 장소의 배열에서도 마찬가지입니다. 이 클래스가 외부 사용자에게 항상 일정한 불변 조건을 유지하도록 보장하고 싶습니다. 이 친숙한 배열을 사용하면 실수로 SomeThing
의 초기화되지 않은 배열을 만들 수 있습니다.
또한, 룩업 테이블은 다소 복잡한 과정을 사용하여 생성되며, std::array<SomeThing, 64> SomeThing::lookup_table_0(some value)
클래스가 움직일 수 있다면'std :: vector'를 사용해보십시오. 움직이지 않으면'std :: deque'가 여전히 작동한다고 생각합니다. – Brian
이 클래스 정의가 잘못 구성되어 있는지, 표준 컨테이너가 완전한 유형으로 만 인스턴스화되어야하는지 궁금합니다. (그리고이 문제는 자체의 템플릿에서 오는 정적 멤버가 포함 된 클래스로 인해 발생합니다.) –
매트 : 표준 컨테이너가 헤더에 아직 구현되지 않았지만 구현에만 있습니다. –