HP Code Advisor Diagnostics Reference Guide (5900-1865, July 2011)
2.133 4286 return non-const handle to non-public data member may
undermine encapsulation
Cause:
A less-restrictive member function is returning a non-const reference or pointer to a more-restrictive
data member. Since the handle thus returned is not a const, the caller will be able to modify the
restrictive data member, thus violating the norms of encapsulation.
Example:
class Capsule {
public:
int& getvalue_ref() { return private_data; }
int* getvalue_ptr() { return }
private:
int private_data;
} object;
Action:
Declare the member functions to return a const handle instead.
Reference:
2.134 4289 endian porting: the definition of the union may be endian
dependent
Cause:
The values accessed using the members of a union may differ based on endianness of a machine
in which the code is executed. This occurs when the union has members of different sizes, and
when the value stored using one member is accessed using another of a different size.
Example:
union Endian { char c[4]; int v; }; // warning 4289 here
int foo() {
union Endian u = { 0x11, 0x22, 0x33, 0x44 };
int i = u.v;
return i;
}
// On a big endian machine value of i will be 0x11223344 whereas
// on a little endian machine it will be 0x44332211
Action:
For portable code, change the way this union is used. Do not use unions for converting values
from one type to another.
Reference:
2.135 4290 endian porting: the initialization of char array may be endian
dependent
Cause:
A char array is being initialized using hexadecimal values. It is likely that this array will later be
accessed as an integer. Based on endianness of a machine on which code is executed the value
of this integer will differ.
Example:char a[4] = { 0x11, 0x22, 0x33, 0x44 }; // warning 4290 here
Action:
For portable code, check how this array is used.
Reference:
2.133 4286 return non-const handle to non-public data member may undermine encapsulation 63