|
![]() Click on image to see enlargment |
|
PC-lint/FlexeLint Output | Reference Manual Explanation | Home bug1763.cpp
At our New Year's Eve party, Letter Man, accessing our Drink through a const function, turned our "Martini" into a "Mastini". How was he able to get away with this? bug1763.cpp lint Output
--- Module: bug1763.cpp
_
char & get(int i) const { return name[i]; }
bug1763.cpp(8) : Info 1763: Member function 'Drink::get(int) const' marked as
const indirectly modifies class
_
};
bug1763.cpp(9) : Info 1712: default constructor not defined for class 'Drink'
_
Drink Martini("Martini");
bug1763.cpp(15) : Info 1776: Converting a string literal to char * is not const
safe (arg. no. 1)
Reference Manual Explanation
1763 Member function 'Symbol' marked as const indirectly modifies class -- The
designated symbol is a member function declared as const. Though technically
valid, the const may be misleading because the member function modifies
(or exposes) information indirectly referenced by the object. For example:
class X
{
char *pc;
char & get(int i) const { return pc[i]; }
};
results in Info 1763 for function X::get. This is because the function
exposes information indirectly held by the class X.
Experts [24] recommend that a pair of functions be made available in
this situation:
class X
{
char *pc;
const char & get(int i) const { return pc[i]; }
char & get(int i) { return pc[i]; }
};
In this way, if the object is const then only the const function will
be called, which will return the protected reference. Related messages
are also 1762 and 1962.
If you have comments or questions about this bug, please post them to our Discussion Forum |
Previous Bug - Bug #815 - December 2004