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

            bug1536.cpp

1     #include <stdio.h>
2
3     class A
4         {
5         int count;
6       public:
7         A() :count(0) {}
8         void bump() { count++; }
9         int &usage() { return count; }
10        };
11    void report( int &n ) { n *= 2; }
12    int main()
13        {
14        A a;
15        a.bump();
16        a.bump();
17        report( a.usage() );
18        printf( "%d\n", a.usage() );
19        return 0;
20        }

The programmer expected the printout to show a usage of 2 but instead got a usage of 4. The villain in the piece is obvious but why did a private value get modified by a public agent and why was the compiler silent?


bug1536.cpp lint Output

--- Module:   bug1536.cpp
                               _
    int &usage() { return count; }
bug1536.cpp(9) : Warning 1536: Exposing low access member 'A::count'


Reference Manual Explanation

 
1536   Exposing low access member 'Symbol'  -- A member function is
       returning the non-const address of a member either directly or
       via a reference.  Moreover, the member's access (such as private
       or protected) is lower than the access of the function returning
       the address.  For example:

                 class X
                     {
                   private:
                     int a;
                   public:
                     int *f() { return &a; }
                     };

       This looks like a breach of the access system [12, Item 30].  You
       may lower the access rights of the function, raise the
       accessibility of the member or make the return value a const
       pointer or reference.  In the above example you could change the
       function to:

                 const int *f() { return &a; }


Previous Bug - Bug #534 - April 2001

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright © 2003, Gimpel Software