Your polymorphic assignment operator isn't working because the declaration in the derived class is different than the one in the base class. First of all, the assignment operator should return a reference, so change it to:
virtual Cliente &operator=(Cliente const &cliente);
And then use that SAME declaration in the child classes:
Cliente &operator=(Cliente const &cliente);
The implementation for a child class will look something like this:
Cliente &Persona::operator=(Cliente const &cliente)
{
if (this == &cliente)
return *this;
//See if the object being copied is another Persona
const Persona *pOther = dynamic_cast<const Persona *>(&cliente);
// if pOther is not null, cast was successful
if (pOther)
{
// Copy derived class attributes
// this->x = pOther->x, etc.
}
// Call base class operator=, to copy base class attributes
return Cliente::operator=(cliente);
}
You can also define a second assignment operator for the derived class, using the derived type (for example, Persona &operator=(Persona const &persona)
). But the one which will be used in your example is the one that takes a Cliente const &
as a parameter.