After overloading the = operator,
const Warrior& Warrior::operator = (const Warrior& warriorObj)
{
if (this != &warriorObj)
{
name = warriorObj.name;
start = warriorObj.start;
alignment = warriorObj.alignment;
strength = warriorObj.strength;
craft = warriorObj.craft;
gold = warriorObj.gold;
life = warriorObj.life;
fate = warriorObj.fate;
specialAbility = warriorObj.specialAbility;
numberOfObjects = warriorObj.numberOfObjects;
}
return *this;
}
Once i create 2 Warriors in the driver:
Warrior *w1, *w2;
w1 = new Warrior();
w2 = new Warrior();
w1 = w2;
If I change one of the parameters of w1, it also changes the parameter of w2 to the same thing…
Where did i go wrong?
The overloaded operator doesn’t get called since you’re assigning pointers. You can use
or, better yet, don’t use dynamic objects at all: