你好,我有一个Ball课程。
class Ball: public Pang::particle {
private:
double radio;
double rcolor, gcolor, bcolor; // Range 0.0-1.0 Color (given in RGB space)
继承自粒子:
class particle {
protected:
double mass;
vector3 position;
vector3 velocity;
vector3 acceleration;
vector3 forceAccumulator;
int state;
double time_remaining;
public:
particle();
particle(const particle &rhs)
{
mass=rhs.mass;
position = rhs.position;
velocity = rhs.velocity;
acceleration = rhs.acceleration;
forceAccumulator = rhs.forceAccumulator;
state = rhs.state;
time_remaining = rhs.time_remaining;
}
所以我怀疑在这里我有一个pangScenario类:
namespace Pang {
class PangScenario {
friend class ReflexAgentAI;
friend class SimulationAI;
private:
//Attributes are:Four planes, each one representing a plane ( scenario margin). el valor de position
//Plane top;
Plane bottomBorder;
Plane leftBorder;
Plane rightBorder;
double limitPlane;
//A ball positioned on it.
//Ball scenarioBall;
vector<Ball> playingBalls;
character* characterPlayerOne;
character* characterPlayerTwo;
Ball* projectilePlayerOne;
Ball* projectilePlayerTwo;
double lastShotPlayerOne;
double lastShotPlayerTwo;
double lastDeadPlayerOne;
double lastDeadPlayerTwo;
int scorePlayerOne;
int scorePlayerTwo;
int livesPlayerOne;
int livesPlayerTwo;
int numberVictoriesPlayerOne;
int numberVictoriesPlayerTwo;
int numberTies;
double widthGameField; // in meters
double heightGameField; // in meters
//friend ReflexAgentAI;
public:
PangScenario();
PangScenario(double width, double height);
PangScenario(const PangScenario &rhs);
virtual ~PangScenario()
{
if (projectilePlayerOne) delete projectilePlayerOne;
if (projectilePlayerTwo) delete projectilePlayerTwo;
if (characterPlayerOne) delete characterPlayerOne;
if (characterPlayerTwo) delete characterPlayerTwo;
//delete playingBalls;
}
因此,当我在PangScenario中使用向量时,是否需要在PangScenario的析构函数中向向量playBalls添加析构函数?如果是这样我该怎么办?
类似的东西:
for(int i=0 ; i < playingBalls.size(); ++i) {
//pd = *it;
playingBalls.erase(i); or delete playingBalls[i];
}
也许很简单:playingBalls.clear();
在PangScenario
提前致谢
答案 0 :(得分:0)
您无需破坏vector
。它是一个对象,而不是指向对象的指针,因此您不负责清理它。当PangScenario
类的实例超出范围时,其所有成员变量都将被销毁。
如果您有vector<Ball> *
,那么您需要关注delete
- 为您分配的任何内存。
最后,您也不需要clear
向量。 vector
自己的析构函数将负责清理自己。