MISRAC++2008-9-3-2 (C++ only)
In this section:
Synopsis
(Required) Member functions shall not return non-const handles to class-data.
Enabled by default
Yes
Severity/Certainty
Medium/High

Full description
Member functions return non-const handles to members. This check is identical to CPU-return-ref-to-class-data.
Coding standards
- CERT OOP35-CPP
Do not return references to private data
Code examples
The following code example fails the check and will give a warning:
class C{
int x;
public:
int& foo();
int* bar();
};
int& C::foo() {
return x; //returns a non-const reference to x
}
int* C::bar() {
return &x; //returns a non-const pointer to x
}
The following code example passes the check and will not give a warning about this issue:
class C{
int x;
public:
const int& foo();
const int* bar();
};
const int& C::foo() {
return x; //OK - returns a const reference
}
const int* C::bar() {
return &x; //OK - returns a const pointer
}