I`m implementing move constructor and operator= for my class and I wish to implement moving only once inside operator=(). But member object constructed constructed before constructor body executes and compilation fails because it has no default constructor.
Initialization list will work, but requires me to write move code twice. Also I could add default constructor for the Sprite but... It will construct Sprite and then destroy it to apply move assignment. It seems not good (and requires me to have default constructor for the class not intended to have such).
How could I solve this problem. Or maybe there is another practice to put all move code in single place? BTW, same problem with copying. Thanks!
class SceneObject {
private:
Sprite m_Sprite; // Has no default constructor!
public:
inline SceneObject (const SceneObject&& val) { *this = std::move(val); }
//inline SceneObject (const SceneObject&& val) : m_Sprite(val.m_Sprite) {} -- this works!
inline SceneObject& operator= (const SceneObject&& val) {
m_Sprite = std::move(val.m_Sprite);
return *this;
}
};
Aucun commentaire:
Enregistrer un commentaire