Click on image to see enlargment

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

            bug1538.cpp

1    #include <stdio.h>
2
3    class Flower
4        { public:
5        Flower( ) : color("colorless") { }
6        Flower(const Flower & f) :color(f.color) { }
7        const char *color;
8        };
9
10   class Rose : public Flower
11       { public:
12       Rose() { color = "red"; }
13       Rose( const Rose & ) { }
14       };
15
16   void print( const Rose r )
17       {
18       printf( "roses are %s\n", r.color );
19       }

This is part of a program written by a florist. Its purpose is to assess the color of various flowers. He was shocked, though, when the program declared that roses were colorless. Where did he goof?


bug1538.cpp lint Output

--- Module:   bug1538.cpp (C++)
                                           _
    Flower(const Flower & f) :color(f.color) { }
bug1538.cpp(6) : Warning 1554: Direct pointer copy of member 'Flower::color'
    within copy constructor: 'Flower::Flower(const Flower &)'
                         _
    Rose( const Rose & ) { }
bug1538.cpp(13) : Warning 1538: base class 'Flower' absent from initializer
    list for copy constructor
    _
    }
bug1538.cpp(19) : Info 1746: parameter 'r' in function 'print(Rose)' could be
    made const reference

Reference Manual Explanation


1538   base class 'Name' absent from initializer list for copy constructor -- The
       indicated base class did not appear in the initializer list for a copy
       constructor.  Was this an oversight?  If the initializer list does not
       contain an initializer for a base class, the default constructor is used
       for the base class. This is not normally appropriate for a copy constructor.
       The following is more typical:

                 class B { ... };
                 class D : public B
                     {
                     D( const D &arg ) : B( arg ) { ... }
                     ...
                     };


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


Previous Bug - Bug #505 - April 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.