나는 그 클래스 A
및 B
더 ..이컴파일 타임에 일정한 크기의 배열 ..하지만 파생 클래스에서 만
class A : public O
{
static const int _nbItems = 3;
...
};
class B : public O
{
static const int _nbItems_mine_mine = 7800;
...
};
...
그들은 추상적 부모 클래스 O
모든 inheritate하는 는 .. Item
의 수집을 관리하고 그들이 원하는대로 아이들이 놀러 못하게하는 방법 알고
class O
{
private:
// The kids won't be able to access this structure directly for it is carefully updated
Item _items[size];
// (I mean.. *truly*, look :)
void update() // called by the clock
{
for(int i(0); i < size; ++i) /* things involving */ _items[i] /*, its environment etc.*/
};
protected:
// They may set their items this way only:
void setItemNo(int id, BuildInformation const& buildInfo)
{
/*
* check whether or not the item has already been added..
* perform everything that must be done to welcome a new item..
* well.. 'so many things the kids do not need to be aware of.
*/
// and then:
_items[id] = Item(buildInfo);
};
// .. retrieve information about the current state of their items this way only:
SomeInformation getItemState(int id) const {return _items[id].currentState();};
// .. and eventually change some of their properties this way only:
void setItemProperty(int id, Property const& newProperty)
{
/* checks, updates, eventual repercussions on other items and the environment */
_items[id].setProperty(newProperty);
};
};
O::_items
에 대한 구조로 사용하여 스택에 할당 된 Item
이 모두에 할당 된 구조로 사용할 수 있습니까? 크기가 궁극적으로 컴파일 시간에 알려지기 때문에 가능한 것이어야합니다.
*nbItems*
정보가 아직 정의되지 않은
O
에, 컴파일러 그들은 여전히 문자 상수 알고하자 패션에
O::size
까지 할 수 있을까?
추신 : O
가 기록되는 시간, 나는 분명히 언젠가는있을 수있는 모든 가능한 파생 클래스를 인식하지입니다. 중간 파생 클래스 다음
class O
{
virtual size_t size() const = 0;
virtual Item * data() const = 0;
void update()
{
for (Item * p = data(), * e = p + size(); p != e; ++p)
{
// use *p
}
}
public:
virtual ~O() {}
};
: 다음
template <size_t N> class OWithArray : public O
{
public:
static size_t const nItems = N;
private:
Item items[N];
virtual size_t size() const { return nItems; }
virtual Item * data() const { return items; }
};
그리고는 모두 실제 파생 클래스가 중간 클래스에서 파생 :
'O'가'size'에서 매개 변수화 된 템플릿 클래스가 될 수없는 이유가 있습니까? –
'O' 템플릿을 만듭니다. '템플릿 클래스 O {Item _item [size]; }'이면 파생 클래스는'O <3>'또는'O <7800>'에서 파생 될 수 있습니다. –
@CharlesBailey : 오, 그렇지 않을 수도 있습니다! 이걸 더 생각해 봅시다. :) –