我建议你有2个类:
数据类“Precious”,用于保存影响类的单个成员的属性、访问器和操作。
class Treasure; // let's pre-declare this.
class Precious {
private:
BOOL invisible;
Precious() : invisible(FALSE) {}
friend class Treasure. // so it can call the private constructor
public:
BOOL isMine() { return TRUE; }
BOOL iWantsIt() { return TRUE; }
void don() { invisible = TRUE; }
~Precious { printf("need to find mnt/doom here"); exit(1); }
}
一个集合和管理类“Treasure”,它包含创建新成员并将它们添加到列表中的方法,以及跨这些列表管理和执行操作的方法。
class Treasure {
void forgeOne { rings.add( new Precious ); };
void findThemAll {
for (Precious* myPrecious;
{
// implement remote ring call
}
}
void inTheDarknessBindThem {
// need to add check for the one ring
for (Precious* myPrecious;
{
// filter on dark rings
// add binding code.
}
}
Map<Precious*> rings;
}
然后,您可以将 Treasure 的实例构造为全局或单例并根据需要访问它。