Recall: An
lvalue
is anything with an address Anlvalue reference
is like a const ptr with auto-dereferencing
using namespace std;
struct Node{
int data;
Node *next;
~Node(){
delete next;
}
};
Node oddsOrEvens () {
Node odds{1,new Node{3,new Node{5,nullptr}}};
Node evens{2, new Node{4, new Node{6, nullptr}}};
char c;
cin >> c;
if(c == '0') return evens;
else return odds;
}
Node n{oddsOrEvens()}; // `oddsOrEvens()` returns a temporary object
// note that this calls a copy constructor
OddsOrEvens
other
is a reference to this temporary objectThe temporary is just going to be discarded anyway!! It will happen as soon as the statement Node n {oddsOrEvens())
is done. This is very wasteful. why not just steal it instead? save the cost of copy
So we need to be able to tell whether the other is a reference to a temporary object (where stealing would work) or a standalone object (where we would have a copy)
C++ Rvalue Reference
We use Node&&
to denote a temporary object (rvalue) of type Node
struct Node {
// This is called the move constructor
Node (Node &&other): data {other.data}, next{other.next} {
other.next = nullptr; // stealing other's data (SUPER IMPORTANT)
}
}
Operator Overloading
We can do the following using rvalue reference
struct Node {
Node &operator=(Node &&other) { // steal other's data
std::swap(data, other.data); // destroy my old data
std::swap(next, other.next); // swap without copy
return *this;
}
}// temp will be destoryed and take my old data with it