This problem is really odd, I can't seem to find anything that causes it.
So here is an assignment operator overloading function, birds and mammals are vectors. (down below are the classes)
const Register& Register::operator=(const Register& theOther)
{
if(this != &theOther)
{
this->~Register(); // deleted
birds.resize(theOther.birds.size());
mammals.resize(theOther.mammals.size());
birds=theOther.birds;
mammals=theOther.mammals;
}
return *this;
}
(if you find anything that is weird here, that is because I rewrote these lines many times). So the problem is while running, the
birds.operator=(theOther.birds);
works absolutely fine, every variable is copied properly, but when it reaches the next line, it calls the same vector copying function as with the birds, but mammals never gets theOther.mammals -es Animal parts (species, etc.). It gets the only mammal parts (predator, etc.). I just don't understand. Below are my classes, and as you can see, they are completely similar at the parts which are important here...
So, the classes
class Animal
{
char* species;
unsigned int weight;
char* color;
public:
...
}
The bird class
class Bird: public Animal
{
bool singing;
bool waterfowl;
unsigned int eggs;
public:
...
const Bird& operator =(const Bird& theOther);
{
if(this != &theOther)
{
Bird copy(theOther);
this->setSpecies(copy.getSpecies());
this->setWeight(copy.getWeight());
this->setColor(copy.getColor());
singing = theOther.singing;
waterfowl = theOther.waterfowl;
eggs = theOther.eggs;
}
return *this;
}
The mammal class
class Mammal: public Animal
{
bool predator;
bool oviparous;
public:
...
const Mammal& Mammal::operator =(const Mammal& theOther)
{
if(this != &theOther)
{
Mammal copy(theOther);
this->setPredator(copy.getPredator());
this->setOviparous(copy.getOviparous());
predator = theOther.predator;
oviparous = theOther.oviparous;
}
return *this;
}
And of course the Register, which has the vectors.
class Register
{
std::vector <Bird> birds;
std::vector <Mammal> mammals;
std::vector <Reptile> reptiles;
public:
...
edit:
Register::Register(const Register& newRegister)
{
birds = newRegister.birds;
mammals = newRegister.mammals;
reptiles = newRegister.reptiles;
}
edit2:
void Animal::setSpecies(char* _species)
{
this->species = _species;
}
Maybe I'm just too tired to find the mistake that I made. Please tell me what I did wrong.