重载 = 运算符后,
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;
}
一旦我在驱动程序中创建了 2 个勇士:
Warrior *w1, *w2;
w1 = new Warrior();
w2 = new Warrior();
w1 = w2;
如果我更改 的参数之一w1
,它也会将 的参数更改为w2
相同的东西...
我哪里做错了?