以下に説明するクラスを取得しました
class Investment
{
private:
void init(BWAPI::UnitType type1, BWAPI::UpgradeType type2, BWAPI::TechType type3, int numOfItems);
UpgradeType upgradeType;
TechType techType;
UnitType unitType;
私のinitメソッドはそのようなものです
void Investment::init(BWAPI::UnitType type1, BWAPI::UpgradeType type2, BWAPI::TechType type3, int numOfItems)
{
if (type1 != NULL) {
this->unitType = type1;
this->type = type1.isBuilding() ? BUILDING_TYPE : UNIT_TYPE;
} else if (type2 != NULL) {
this->upgradeType = type2;
this->type = UPGRADE_TYPE;
} else if (type3 != NULL) {
this->techType = type3;
this->type = TECH_TYPE;
}
this->numOfItems = numOfItems;
}
この方法でアイテムを取得します (3 つの可能なタイプのうちの 1 つにすぎません)。
const void* Investment::getItem() const
{
if (unitType != NULL)
return &unitType;
else if (upgradeType != NULL)
return &upgradeType;
else if (techType != NULL)
return &techType;
return NULL;
}
しかし、ポインターを使用してアイテムを取得するとどうなるか、UnitType* type = (UnitType*) investment->getItem();
その値を失います
呼び出すtype->getName().c_str();
と、空の文字列が返されます
投資を得るために Stack のメソッドを呼び出します
// I have a stack of type std::vector<T*> stack;
T* getNext() {
clear();
for (int i = 0, leni = stack.size(); i < leni; i++) {
T* t = stack.at(i);
if (!t->isDone() && !t->isCanceled())
return t;
}
return NULL;
}