lundi 23 mai 2022

move constructor and move assignment

I have this code

class GameViewModel
{
public:
    GameViewModel(PlayerData& playerData, std::unique_ptr<WeaponData> WeaponData) : m_playerData(playerData), m_WeaponData(std::move(WeaponData)){}

    PlayerData& GetPlayerData() const { return m_playerData; }
    // I want to do:
    //void SetPlayerData(PlayerData playerData) { m_playerData = playerData); }
    WeaponData& GetWeaponData() const { return *m_WeaponData; }
    
    bool IsVisible() const;
    int GetColour() const { return m_playerData.m_Envelope.GetColour(); }

private:
    PlayerData& m_playerData;
    std::unique_ptr<WeaponData> m_WeaponData;
};

I'd like to update the member variable m_playerData, (see SetPlayerData) but I get a C2280 attempting to ref a deleted function. I need to define my own move constructor and move assignment. I've looked at: Copy constructor for a class with unique_ptr but how do I initialize m_playerData in the move constructor?

Aucun commentaire:

Enregistrer un commentaire