class MoveSemantic{
public:
// For inserting a value
MoveSemantic(short &value);
MoveSemantic(short &&value);
short &storedValue;
};
// Reference forced to initialize
MoveSemantic::MoveSemantic(short &value) : storedValue(value) {}
MoveSemantic::MoveSemantic(short &&value) : storedValue(value) {}
// Passing a RValue => Assiging to reference => Is this safe ?
MoveSemantic semantik(100);
cout << semantik.storedValue; // 100
I lately discovered that its possible to assign a temporary value to a reference inside a class by abusing the rvalue semantics... But why is this possible ? How does the memory behaves ?
Does this extend the lifetime of the temporary variable "100" due to its assigning ? How safe is this ? And when does the temporary value gets destroyed by the scope ?
Aucun commentaire:
Enregistrer un commentaire