Click on image to see enlargment

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

            bug1772.cpp

1    #include <stdio.h>
2    class R
3        {
4        public:
5        int k, depth;
6        R() :k(0), depth(0) {}
7        const R&  operator=( const R & r )
8            {
9            if( this != &r )
10               { k = r.k; depth = r.depth+1; }
11           return r;
12           }
13       };
14   int main()
15       {
16       R r1, r2, r3;
17       r1 = r2 = r3;
18       printf( "%d %d %d\n",
19               r1.depth, r2.depth, r3.depth );
20       }

Wacknia is attempting to simulate the recording industry where a copy of a recording R is less faithful than the original. Member depth is meant to record the degree of imperfection. But to her surprise the program above is printing "1 1 0" rather than the expected "2 1 0". What's going on?


bug1772.cpp lint Output

--- Module:   bug1772.cpp (C++)
                _
        return r;
bug1772.cpp(11) : Info 1772: Assignment operator 'R::operator=(const R &)' is
    not returning *this

Reference Manual Explanation


1772   Assignment operator 'Symbol' is not returning *this  -- The assignment operator
       should return *this.  This is to allow for multiple assignments as in:

             a = b = c;

       It is also better to return the object that has just been modified rather than the argument.


If you have comments or questions about this bug, please post them to our   Discussion Forum


Previous Bug - Bug #647 - October 2007

[ 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 2007, Gimpel Software, All rights reserved.