Tuesday, 23 April 2013

References and const


const references can be initialised with non-const objects; the object referred to will not
be modifiable through the reference. Plain references cannot be initialised with a const
object, as that would allow a const object to be changed.
{
int i = 1;
const int j = 2;
const int& k = i; // OK
int& l = j; // illegal
k = 3; // illegal, even though reference is
// to i which is not const

No comments:

Post a Comment