Rule Definition
From a language point of view, the pointer 'p' is part of the class and then cannot be modified in a 'const' function. But the pointed-to value is not part of the class, and may be modified. However, from a design point of view, in this case the pointed to value is still logically part of the class, it should not be modified in a 'const' function, nor should a 'const' function allow its caller to modify it through a reference.
Remediation
To correct the violation, return a 'const' reference, or return by value.
Violation Code Sample
class A
{
public:
A() : p(new int(0)) {}
~A() {delete p;}
int &violation() const {return *p;} // Violation
int *p;
};
void f(A const &a)
{
a.violation() = 42;
}
Fixed Code Sample
class A
{
//...
int const &correct1() const {return *p;}
int correct2() const {return *p;}
}
Reference
High Integrity C++ Coding Standard Manual
Related Technologies
C++
Technical Criterion
Programming Practices - Modularity and OO Encapsulation Conformity
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.