
...
Code Block |
---|
error: 'const' qualifier may not be applied to a reference |
...
Noncompliant Code Example
This compliant solution assumes the programmer intended for the previous noncompliant code example to fail to compile due to attempting to modify a correctly declares p
to be a reference to a const-qualified char
reference:. The subsequent modification of p
makes the program ill-formed.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <iostream> void f(char c) { const char &p = c; p = 'p'; // Error: read-only variable is not assignable std::cout << c << std::endl; } |
Compliant Solution
This compliant solution removes the const
qualifier.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <iostream>
void f(char c) {
char &p = c;
p = 'p';
std::cout << c << std::endl;
}
|
...