飞船
#include <iostream>
#include <string>
#include <math.h>
#include <vector>
class Position {
public:
Position (std::string Name, double x, double y, double z);
private:
std::string Name;
double x, y, z; //Coordinates on a 3D map
};
class Flight {
public:
Flight (Position *Start, Position *End);
~Flight();
private:
Position *Start;
Position *End;
};
class Spaceship {
public:
Spaceship (std::string Name, Flight *flightPlan);
~Spaceship();
private:
std::string Name;
Flight *flightPlan;
};
class Universe {
public:
Universe ();
~Universe ();
void addSpaceship (Spaceship *Spaceship);
private:
std::vector <Spaceship*> *Spaceships; // A vector of Spaceship pointers
std::vector <Spaceship*>::iterator start, end;
int numberOfSpaceships;
};
spaceship
#include "Spaceship.h"
Position::Position (std::string Name, double x, double y, double z) {
this->Name = Name;
this->x = x;
this->y = y;
this->z = z;
}
//------------------------------------------------------------
Flight::Flight (Position *Start, Position *End) {
this->Start = Start;
this->End = End;
}
Flight::~Flight () {
delete this->Start;
delete this->End;
}
//--------------------------------------------------------------
Spaceship::Spaceship (std::string Name, Flight *flightPlan) {
this->Name = Name;
this->flightPlan = flightPlan;
}
Spaceship::~Spaceship () {
delete this->flightPlan;
}
void Spaceship::printFlightPlan () {
std::cout << "Spaceship name: " << this->Name << std::endl;
this->flightPlan->printFlightPlan();
}
//---------------------------------------------------------------
Universe::Universe () {
this->Spaceships = new std::vector <Spaceship*>();
this->start = this->Spaceships->begin();
this->end = this->Spaceships->end();
this->numberOfSpaceships = Spaceships->size();
}
Universe::~Universe() {
delete this->Spaceships;
}
void Universe::addSpaceship(Spaceship *Spaceship) {
this->Spaceships->push_back(Spaceship);
this->start = this->Spaceships->begin();
this->end = this->Spaceships->end();
}
int main (void) {
Position *Start = new Position("Home", 45.566, 34.5677, -12.4565);
Position *End = new Position ("Far Far Away", 67.4564, -56.6765, 23.4565);
Flight *virginFlight = new Flight(Start, End);
Spaceship *sp1 = new Spaceship("Virgin Mary", virginFlight);
Spaceship *sp2 = new Spaceship("Something else", virginFlight);
Universe *alpha = new Universe();
alpha->addSpaceship(sp1);
alpha->addSpaceship(sp2);
delete alpha;
return 0;
}
好的,这应该是我代码的最干净的版本。它只有构造函数和析构函数。如果我错过了一些变量/函数,请假设它们在那里。这样可以编译运行。我唯一的问题是内存泄漏。
Valgrind报告分配>删除至少2倍。
最佳答案
实际上,您的程序泄漏了。这是由于代码中的spaceship * std::vector。仅当STL vector 容器包含对象而不是对象的指针时,它才会管理并释放其内部内存。
在这里,当您向 vector 添加Spaceship并删除了alpha时,不会在主程序中释放sp1和sp2指向的内存。
在现代C++中,应避免使用原始指针,而应尝试使用智能指针。在这里,我试图更改您的代码,我认为现在不会泄漏。
spaceship
#include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <memory>
class Position {
public:
Position(std::string Name, double x, double y, double z);
private:
std::string Name;
double x, y, z; //Coordinates on a 3D map
};
class Flight {
public:
Flight(Position *Start, Position *End);
~Flight();
void printFlightPlan();
private:
Position *Start;
Position *End;
};
class Spaceship {
public:
Spaceship(std::string Name, Flight *flightPlan);
~Spaceship();
void printFlightPlan();
private:
std::string Name;
Flight *flightPlan;
};
class Universe {
public:
Universe();
~Universe();
void addSpaceship(std::shared_ptr<Spaceship>);
private:
std::vector <std::shared_ptr<Spaceship>> *Spaceships; // A vector of Spaceship pointers
std::vector <std::shared_ptr<Spaceship>>::iterator start, end;
int numberOfSpaceships;
};
#include "Spaceship.h"
Position::Position(std::string Name, double x, double y, double z) {
this->Name = Name;
this->x = x;
this->y = y;
this->z = z;
}
//------------------------------------------------------------
Flight::Flight(Position *Start, Position *End) {
this->Start = Start;
this->End = End;
}
Flight::~Flight() {
/*delete this->Start;
delete this->End;*/
}
void Flight::printFlightPlan() {
}
//--------------------------------------------------------------
Spaceship::Spaceship(std::string Name, Flight *flightPlan) {
this->Name = Name;
this->flightPlan = flightPlan;
}
Spaceship::~Spaceship() {
delete this->flightPlan;
}
void Spaceship::printFlightPlan() {
std::cout << "Spaceship name: " << this->Name << std::endl;
this->flightPlan->printFlightPlan();
}
//---------------------------------------------------------------
Universe::Universe() {
this->Spaceships = new std::vector<std::shared_ptr<Spaceship>>;
this->start = this->Spaceships->begin();
this->end = this->Spaceships->end();
this->numberOfSpaceships = Spaceships->size();
}
Universe::~Universe() {
//delete this->Spaceships;
}
void Universe::addSpaceship(std::shared_ptr<Spaceship> Spaceship) {
this->Spaceships->push_back(Spaceship);
this->start = this->Spaceships->begin();
this->end = this->Spaceships->end();
}
int main(void) {
Position *Start = new Position("Home", 45.566, 34.5677, -12.4565);
Position *End = new Position("Far Far Away", 67.4564, -56.6765, 23.4565);
Flight *virginFlight = new Flight(Start, End);
std::shared_ptr<Spaceship> sp1(new Spaceship("Virgin Mary", virginFlight));
std::shared_ptr<Spaceship> sp2(new Spaceship("Something else", virginFlight));
Universe *alpha = new Universe();
alpha->addSpaceship(sp1);
alpha->addSpaceship(sp2);
delete alpha;
return 0;
}