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

            bug1738.cpp

1     #include <iostream.h>
2     struct Art {
3         Art( const Art & ) { original = false; }
4         Art( ) { original = true; }
5         bool original;
6     };
7
8     struct Painting : public Art {
9         Painting( const Painting & arg ) :Art() { name = arg.name; }
10        Painting( const char *nm ) { name = nm; }
11        const char *name;
12        };
13    void print( const Painting &arg )
14        { cout << (arg.original ? "original" : "copy") << '\n' ; }
15
16    void print_copy( Painting arg ) { print( arg ); }
17
18    int main()
19        { Painting a("The Idiot"); print( a ); print_copy( a ); return 0; }

The programmer expected the first string printed to be "original" but was surprised when the second also printed "original". He expected argument passing to invoke a copy constructor that would result in a "copy". Where did the programmer go wrong?


bug1738.cpp lint Output

--- Module:   bug1738.cpp
                                            _
    Painting( const Painting & arg ) :Art() { name = arg.name; }
bug1738.cpp(9) : Info 1738: non copy constructor 'Art::Art(void)' used to
    initialize copy constructor
bug1738.cpp(9) : Warning 1554: Direct pointer copy of member 'Painting::name'
    within copy constructor: 'Painting::Painting(const Painting &)'
    _
    };
bug1738.cpp(12) : Info 1712: default constructor not defined for class
    'Painting'
                                                _
void print_copy( Painting arg ) { print( arg ); }
bug1738.cpp(16) : Info 1746: parameter 'arg' in function 'print_copy(Painting)'
    could be made const reference

Reference Manual Explanation

 
1738  non-copy constructor 'Symbol' used to initialize copy constructor
      -- In an initializer list for a copy constructor, a base class
      constructor was invoked. However, this base class constructor was
      not itself a copy constructor. We expect that copy constructors
      will invoke copy constructors. Was this an oversight or was there
      some good reason for choosing a different kind of constructor? If
      this was deliberate, suppress this message. See also message 1538.

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

Previous Bug - Bug #1732 - January 2003

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

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