| No operator operates on a reference as such. | Lecture 2 - slide 17 : 42 Program 2 |
// Example similar to the function g on page 98 in 'The C++ Programming Language'.
#include <iostream>
#include <string>
using namespace std;
void g(){
int ii = 0;
int& rr = ii; // rr is a reference to ii - an alias to ii
rr++; // ii is incremented.
// The reference is NOT incremented itself.
cout << ii << endl; // 1
cout << rr << endl; // 1
// NOW WE DO SIMILAR THINGS ON POINTERS:
int* pp = &rr; // pp is really the address of ii (via rr) - a pointer to ii.
// NOT a pointer to a reference!
(*pp)++; // ii is incremented again,
pp++; // The pointer as such is incremented - pointer arithmetic.
// Not good...
cout << ii << endl; // 2
cout << (*pp) << endl; // 2673944
cout << (*(pp-1)) << endl; // Still 2
}
int main(){
g();
}