我创建了 deque<CObject*> 并添加了不同类型的元素。我怎样才能改变这个元素的属性?
I created deque<CObject*> and added different type of element. How can I change attribute of this elements?
我创建了:
class CMap:{
public
std::deque<CObject*> obiekty;
}
class CBullet{
public:
int damage;
}
并且我添加了不同类型的元素(例如 Tank、Bullet...)
我想使用 deque
更改属性
for( size_t i=0; i<game->Mapa->obiekty.size(); i++){
if(typeid(*(game->Mapa->obiekty[i]))==typeid(CBullet)){
this->HP=this->HP - game->Mapa->obiekty[i] (?? my problem/I can't do it ?? ->damage);
}
}
首先你需要从基础 class (CObject
) 继承你的派生 classes(CBullet
, CTank
),像这样
//Base clase
class CObject
{
public:
//...
virtual ~CObject(){}; // Don't forget about virtual desctructor
};
class CBullet: public CObject
{
public:
//...
~CBullet(){};
void DoBulletMethod(){};
};
class CTank: public CObject
{
public:
//...
~CTank(){};
void DoTankMethod(){};
};
然后你可以通过dynamic_cast
检查所有对象,像这样:
for( size_t i=0; i< game->Mapa->obiekty.size(); i++)
{
if(dynamic_cast<CBullet*>(game->Mapa->obiekty[i]) != nullptr)
{
CBullet * BulletObj = dynamic_cast<CBullet*>(game->Mapa->obiekty[i]);
BulletObj->DoBulletMethod();
}
else if(dynamic_cast<CTank*>(game->Mapa->obiekty[i]) != nullptr)
{
CTank * TankObj = dynamic_cast<CTank*>(game->Mapa->obiekty[i]);
TankObj->DoTankMethod();
}
}
或者您可以使用多态性并创建虚方法,而无需进一步检测当前对象类型,例如在基础 class 中创建一个虚方法并在派生 classes 中覆盖它:
//Base clase
class CObject
{
public:
//...
virtual ~CObject(){}; // Don't forget about virtual desctructor
virtual void DoActionInLoop()
{
//No action in base class
}
};
class CBullet: public CObject
{
public:
//...
~CBullet(){};
virtual void DoActionInLoop() override
{
std::cout << "I am bullet I can do here my tasks" << std::endl;
}
};
class CTank: public CObject
{
public:
//...
~CTank(){};
virtual void DoActionInLoop() override
{
std::cout << "I am tank and I can do here my tasks" << std::endl;
}
};
然后只需在每个 CObject*
实例上调用此方法并获得多态性:
for( size_t i=0; i< game->Mapa->obiekty.size(); i++)
{
game->Mapa->obiekty[i]->DoActionInLoop();
}
我创建了:
class CMap:{
public
std::deque<CObject*> obiekty;
}
class CBullet{
public:
int damage;
}
并且我添加了不同类型的元素(例如 Tank、Bullet...) 我想使用 deque
更改属性for( size_t i=0; i<game->Mapa->obiekty.size(); i++){
if(typeid(*(game->Mapa->obiekty[i]))==typeid(CBullet)){
this->HP=this->HP - game->Mapa->obiekty[i] (?? my problem/I can't do it ?? ->damage);
}
}
首先你需要从基础 class (CObject
) 继承你的派生 classes(CBullet
, CTank
),像这样
//Base clase
class CObject
{
public:
//...
virtual ~CObject(){}; // Don't forget about virtual desctructor
};
class CBullet: public CObject
{
public:
//...
~CBullet(){};
void DoBulletMethod(){};
};
class CTank: public CObject
{
public:
//...
~CTank(){};
void DoTankMethod(){};
};
然后你可以通过dynamic_cast
检查所有对象,像这样:
for( size_t i=0; i< game->Mapa->obiekty.size(); i++)
{
if(dynamic_cast<CBullet*>(game->Mapa->obiekty[i]) != nullptr)
{
CBullet * BulletObj = dynamic_cast<CBullet*>(game->Mapa->obiekty[i]);
BulletObj->DoBulletMethod();
}
else if(dynamic_cast<CTank*>(game->Mapa->obiekty[i]) != nullptr)
{
CTank * TankObj = dynamic_cast<CTank*>(game->Mapa->obiekty[i]);
TankObj->DoTankMethod();
}
}
或者您可以使用多态性并创建虚方法,而无需进一步检测当前对象类型,例如在基础 class 中创建一个虚方法并在派生 classes 中覆盖它:
//Base clase
class CObject
{
public:
//...
virtual ~CObject(){}; // Don't forget about virtual desctructor
virtual void DoActionInLoop()
{
//No action in base class
}
};
class CBullet: public CObject
{
public:
//...
~CBullet(){};
virtual void DoActionInLoop() override
{
std::cout << "I am bullet I can do here my tasks" << std::endl;
}
};
class CTank: public CObject
{
public:
//...
~CTank(){};
virtual void DoActionInLoop() override
{
std::cout << "I am tank and I can do here my tasks" << std::endl;
}
};
然后只需在每个 CObject*
实例上调用此方法并获得多态性:
for( size_t i=0; i< game->Mapa->obiekty.size(); i++)
{
game->Mapa->obiekty[i]->DoActionInLoop();
}