Use move constructor and assignment operator in Chromosome

This commit is contained in:
Joscha 2017-05-24 18:44:52 +00:00
parent fce5da6a94
commit 2995b807f2
2 changed files with 20 additions and 7 deletions

View file

@ -47,8 +47,6 @@ Chromosome::Chromosome(const Chromosome& father, const Chromosome& mother)
Chromosome::Chromosome(const Chromosome& other) Chromosome::Chromosome(const Chromosome& other)
{ {
// reserve to other's size/capacity?
this->genes.reserve(other.genes.size()); this->genes.reserve(other.genes.size());
for (auto& ptr : other.genes) { for (auto& ptr : other.genes) {
@ -59,8 +57,6 @@ Chromosome::Chromosome(const Chromosome& other)
Chromosome& Chromosome::operator=(const Chromosome& other) Chromosome& Chromosome::operator=(const Chromosome& other)
{ {
// reserve to other's size/capacity?
this->genes.clear(); this->genes.clear();
this->genes.reserve(other.genes.size()); this->genes.reserve(other.genes.size());
@ -74,6 +70,22 @@ Chromosome& Chromosome::operator=(const Chromosome& other)
} }
Chromosome::Chromosome(Chromosome&& other)
{
std::swap(this->genes, other.genes);
}
Chromosome& Chromosome::operator=(Chromosome&& other)
{
this->genes.clear();
std::swap(this->genes, other.genes);
return *this;
}
void Chromosome::draw(sf::RenderTarget& target, sf::RenderStates states) const void Chromosome::draw(sf::RenderTarget& target, sf::RenderStates states) const
{ {
for (auto& geneptr : this->genes) { for (auto& geneptr : this->genes) {

View file

@ -20,9 +20,10 @@ public:
Chromosome(); // create empty chromosome Chromosome(); // create empty chromosome
Chromosome(const Chromosome& father, const Chromosome& mother); // crossover Chromosome(const Chromosome& father, const Chromosome& mother); // crossover
Chromosome(const Chromosome& father); // copy constructor Chromosome(const Chromosome& other); // copy constructor
// Chromosome& operator=(const Chromosome& chr) = delete; // copy assignment operator Chromosome& operator=(const Chromosome& other); // copy assignment operator
Chromosome& operator=(const Chromosome& chr); // copy assignment operator Chromosome(Chromosome&& other); // copy constructor
Chromosome& operator=(Chromosome&& other); // copy assignment operator
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;