OOP Pointers
OOP Pointers
= &ci;
// // // // // //
just an ordinary integer uninitialized pointer to integer constant pointer to integer constant integer pointer to constant integer constant pointer to constant integer
When used with references some peculiarities must be considered. See the following example program:
int main() { const int ci = 1; const int &cr = ci; int &r = ci;
// cr = 7; // cannot assign value to constant reference r = 3; // change value of temporary integer cout << "ci == << ci << r == " << r;
What actually happens is that the compiler automatically creates a temporary integer variable with value of ci to which reference r is initialized. Consequently, when changing r the value of the temporary integer is changed. This temporary variable lives as long as reference r. Reference cr is defined as read-only (constant reference). This disables its use on the left side of assignments.