C++ Tips from Thinking in C++(2)

Continued from C++ Tips from Thinking in C++(1)
5. Const pointer VS. Pointer to const
If you want to prevent any changes to the element you are pointing to, that is “Pointer to const”, you write a definition like this:

const int* u;

Starting from the identifier, we read “u is a pointer, which points to a const int.”
Here’s the mildly confusing part. You might think that to make the pointer itself unchangeable, that is, to prevent any change to the address contained inside u, you would simply move the const to the other side of the int like this:

int const* v;

Unfortunately, I will tell you that it’s wrong! Actually “const int* u;EQUALSint const* v;“. However, the way it actually reads is “v is an ordinary pointer to an int that happens to be const.” That is, the const has bound itself to the int again, and the effect is the same as the previous definition. So to avoid confusion, you should abandon the second form.
So how to make a “const pointer”? To make the pointer itself a const, you must place the const specifier to the right of the *, like this:

int d = 1;
int* const w = &d;

Now it reads: “w is a pointer, which is const, that points to an int.” Because the pointer itself is now the const, the compiler requires that it be given an initial value that will be unchanged for the life of that pointer.
You can also make a const pointer to a const object using either of two legal forms:

int d = 1;
const int* const x = &d; // (1)
int const* const x2 = &d; // (2)

Now neither the pointer nor the object can be changed.
6. Standard argument passing
In C it’s very common to pass by value, and when you want to pass an address your only choice is to use a pointer. However, in C++, Your first choice when passing an argument is to pass by reference, and by const reference at that. But, passing by reference produces a new situation that never occurs in C: a temporary, which is always const, can have its address passed to a function.So to allow temporaries to be passed to functions by reference, the argument must be a const reference.The following example demonstrates this: (c++11 introduced

class X {};
X f() { return X(); } // Return by value
void g1(X&) {} // Pass by non-const reference
void g2(const X&) {} // Pass by const reference


int main() {
// Error: const temporary created by f():
//! g1(f());
// OK: g2 takes a const reference:
g2(f());
} ///:~

f( ) returns an object of class X by value. That means when you immediately take the return value of f( ) and pass it to another function as in the calls to g1( ) and g2( ), a temporary is created and that temporary is const. Thus, the call in g1( ) is an error because g1( ) doesn’t take a const reference, but the call to g2( ) is OK.