...
| Code Block |
|---|
|
char &const p;
|
instead of:
| Code Block |
|---|
|
char const& p;
|
If the compiler does not complain of the const reference, the program might be compiled and run and produce surprising results. This is because the first form still allows you to change the character pointed at by p, while the second does not.
...
| Code Block |
|---|
|
char c = 'c';
char &const p = c;
p = 'p';
cout << c << endl;
|
...
On Microsoft Visual C++, this code compiles without incident with a warning diagnostic (warning C4227: anachronism used : qualifiers on reference are ignored) and outputs:
G++ version 4.2.4 refuses to compile the code, complaining:
| Code Block |
|---|
: error: 'const' qualifiers cannot be applied to 'char&'
|
...
| Code Block |
|---|
|
char c = 'c';
const char *const p = &c;
*p = 'p'; // causes compiler error
cout << c << endl;
|
...
| Code Block |
|---|
|
char c = 'c';
const char & p = c;
*p = 'p'; // causes compiler error
cout << c << endl;
|
...