Class 以向量为成员的类析构函数

Class 以向量为成员的类析构函数,class,inheritance,vector,destructor,Class,Inheritance,Vector,Destructor,你好,我有一节舞会课 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;

你好,我有一节舞会课

 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;

  }
所以我的疑问是我有一门课:

    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;
    }
namespace-Pang{
课堂情境{
友人班;
朋友类模拟;
私人:
//属性是:四个平面,每个代表一个平面(场景边界)
//平顶;
平面底部边界;
平面左边界;
平面右边界;
双极限平面;
//放在上面的球。
//球场景球;
矢量玩球;
角色*角色扮演者;
character*characterPlayerTwo;
球*射出物;
球*ProjectlePlayerTwo;
双倍拉斯特朗;
双人拉丝机两台;
双末梢游动器;
双末梢游戏者2;
int计分员;
int scorePlayerTwo;
int-livesPlayerOne;
int livesPlayerTwo;
int NUMBERVICTORIESPLAYRONE;
int NUMBERVICTORIESPLAYERWO;
整数;
双宽度游戏场;//以米为单位
双高游戏场;//以米为单位
//戴相龙;
公众:
假设情景();
(双倍宽度、双倍高度);
PangScenario(const PangScenario&rhs);
虚拟场景()
{
如果(ProjectlePlayeRone)删除ProjectlePlayeRone;
如果(projectleplayertwo)删除projectleplayertwo;
如果(characterplayeron)删除characterplayeron;
如果(characterPlayerTwo)删除characterPlayerTwo;
//删除玩球;
}
因此,当我在PangScenario中使用向量时,我是否需要在PangScenario的析构函数中向向量playingBalls添加一个析构函数?如果是,我该如何做

比如:

   for(int i=0 ; i < playingBalls.size(); ++i) {
  //pd = *it;
  playingBalls.erase(i); or delete playingBalls[i];
}
for(int i=0;i
也许一个简单的:
playingBalls.clear();
场景的析构函数中就足够了


提前感谢

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

如果您有一个
向量*
,那么您需要关注
删除
——删除分配给它的任何内存


最后,您也不需要
清除
向量。
向量的析构函数将自行清理。

所以析构函数可以吗?我也不需要删除playingBalls,因为您可以看到它的注释…感谢这是正确的。您不需要
删除playingBalls
或尝试清理在你的析构函数中,你可以正确地删除需要删除的四个对象。这是什么编程语言?是C++吗?请用你使用的语言来标记你的问题。更新你的问题,点击下面的“链接”。谢谢。