int &c = 1; //Error
const int& d = 1; //OK
int&& e = 1; // double & can be bound to unnamed guy
const int &&e = 1;
int&& gg = e; // Error
int& lala = e // Ok
int& hoho = e = 1; // Ok
name | unname | ||
---|---|---|---|
int& | l value reference | yes | no |
const int& | const l value reference | yes | yes |
int&& | R value reference | no | yes |
R value reference is from C++11
R value reference is for reference of temp objects
const int& a = 1;
const reference initialization
void print_word Word x){ x.print (); }
printWord("Titanic");
argument passing
int func(int x){return x;}
function return value
int x = a + b + c;
evaluation of expressions
temp objects are stored in stack, FILO
difference between const & and &&
A
{
private:
int* p;
A(A&& a)
{
p = a.p;
a.p = nullptr;
}
};
A func(int a)
{
A obj(a);
// return a; //this will use a move constructor(by compiler opt)
return move(obj); // This is the move function in CPP11
}