Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated examples and some wording.

...

Code Block
bgColor#ffcccc
langcpp

  char &const p;

instead of:

Code Block
bgColor#ccccff
langcpp

  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
bgColor#ffcccc
langcpp

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:

Code Block

p

G++ version 4.2.4 refuses to compile the code, complaining:

Code Block

: error: 'const' qualifiers cannot be applied to 'char&'

...

Code Block
bgColor#ccccff
langcpp

char c = 'c';
const char *const p = &c;
*p = 'p'; // causes compiler error
cout << c << endl;

...

Code Block
bgColor#ccccff
langcpp

char c = 'c';
const char & p = c;
*p = 'p'; // causes compiler error
cout << c << endl;

...