I have a class where its constructor takes an rvalue reference to a temporary object
class Lich: public Player {
protected:
int mana;
Spell spell;
public:
Lich(int mana,int health, int defense,Spell&& spell) : spell(spell), mana(mana), Player(health,defense) {}
void cast_spell(Player &target);
};
So far, the constructor of Lich is set to take an rvalue using spell(spell), and since spell is an rvalue, it should be that Spell(Spell&& spell) is the constructor that gets called.
But to my surprise, the copy constructor get's called, and I don't know why
Spell::Spell(Spell&& Spell) noexcept {
std::cout << "move_constructed" << std::endl;
this->cast_time=Spell.cast_time;
this->mp_cost=Spell.mp_cost;
this->damage=Spell.damage;
}
Spell::Spell(Spell& Spell) {
std::cout << "copy_constructed" << std::endl;
this->cast_time=Spell.cast_time;
this->mp_cost=Spell.mp_cost;
this->damage=Spell.damage;
}
int main() {
Lich Arch_Lich(0,0,0,AOE_Spell(0,0,0,0));//AOE_Spell is a subclass from Spell
}
Can anyone explain why is does the compiler choose the copy constructor instead?(-std=c++11 standart used)