【问题标题】:Class destructor with vector as member以向量为成员的类析构函数
【发布时间】:2017-11-28 02:53:22
【问题描述】:

你好,我有一个 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 的析构函数中的向量 playingBalls 中添加析构函数?如果可以,我该怎么做?

类似:

   for(int i=0 ; i < playingBalls.size(); ++i) {
  //pd = *it;
  playingBalls.erase(i); or delete playingBalls[i];
}

也许很简单:playingBalls.clear(); 在PangScenario 的析构函数中就足够了

提前致谢

【问题讨论】:

  • 这是什么编程语言?是 C++ 吗?请用使用的语言标记您的问题。要更新您的问题,请点击帖子下方的 "edit" 链接。谢谢。

标签: class inheritance vector destructor


【解决方案1】:

您不需要破坏vector。它是一个对象,而不是指向对象的指针,因此您不负责它的清理。当PangScenario 类的实例超出范围时,它的所有成员变量都会被销毁。

如果你有一个vector&lt;Ball&gt; *,那么你需要关注delete-ing 你为它分配的任何内存。

最后,您也不需要clear 向量。 vector 自己的析构函数会自行清理。

【讨论】:

  • 所以析构函数没问题?我也不需要删除打球,因为你可以看到它的评论......谢谢
  • 没错。您不需要delete playingBalls 或尝试以任何方式清理它。在您的析构函数中,您正确删除了需要删除的四个对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 2019-03-15
  • 2019-03-09
  • 2023-02-09
  • 1970-01-01
  • 2020-07-15
  • 2012-12-01
相关资源
最近更新 更多