Click on image to see enlargment

PC-lint/FlexeLint Output | Reference Manual Explanation | Home

            bug1763.cpp

1    #include <stdio.h>
2
3    class Drink
4        {
5        public:
6        Drink( char *name_ ) : name( name_ ) {}
7        char *name;
8        char & get(int i) const { return name[i]; }
9        };
10
11   void stir( char &ch ) { ++ch; }
12
13   int main()
14       {
15       Drink Martini("Martini");
16       stir( Martini.get(2) );
17       printf( "%s\n", Martini.name );
18       return 0;
19       }    

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

[ new ] Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2006, Gimpel Software, All rights reserved.