Strange gcc warning messages with derived classes

This is a discussion on Strange gcc warning messages with derived classes within the C++ Programming forums, part of the General Programming Boards category; Hello! I am using gcc 4.1.2 to compile the following code. The base class just makes sure that freed instances ...

  1. #1
    Optics with a Twist skewray's Avatar
    Join Date
    Dec 2006
    Location
    Claremont, CA
    Posts
    31

    Strange gcc warning messages with derived classes

    Hello! I am using gcc 4.1.2 to compile the following code. The base class just makes sure that freed instances are not referenced, and might warn of wild pointers. The label is just a derived class. I compressed the code quite a lot to make it compact; apologies in advance:
    Code:
    #define HAS_ERROR
    
    #include <cassert>
    
    class ExprBase
        {
    public:
        ExprBase() throw() : alloc_(true) {}
        virtual ~ExprBase() throw()
            {
            assert( alloc_ == true ) ;
            alloc_ = false ;
            }
        virtual ExprBase* Copy() const = 0 ;
    private:
        bool alloc_ ;
        } ;
    
    class TAG ;
    
    class Label:public ExprBase
        {
    public:
        Label( const TAG* t ) throw() : tag_(t) {}
        Label( const Label& label ) throw() : tag_(label.tag_) {}
        Label& operator=( const Label& label ) throw() ;
        ~Label() throw() {}
    #ifdef HAS_ERROR
        ExprBase* Copy() const ;
    #else
        ExprBase* Copy() const
        {
        return new Label(tag_) ;
        }
    #endif
    
    private:
        const TAG* tag_ ;
        } ;
    
    #ifdef HAS_ERROR
    ExprBase* Label::Copy() const
        {
        return new Label(tag_) ;
        }
    #endif
    The output of gcc, with every possible warning flag enabled, looks like this:

    g++ -pedantic -Wall -Wabi -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Weffc++ -Wstrict-null-sentinel -Wold-style-cast -Woverloaded-virtual -Wsign-promo -Winit-self -Wswitch-default -Wswitch-enum -Wunused-parameter -Wextra -Wunknown-pragmas -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wsign-compare -Wunreachable-code -c -o copy.o copy.cc
    copy.cc: In copy constructor ‘Label::Label(const Label&)’:
    copy.cc:26: warning: base class ‘class ExprBase’ should be explicitly initialized in the copy constructor
    copy.cc: In destructor ‘virtual Label::~Label()’:
    copy.cc:28: warning: will never be executed
    copy.cc: In destructor ‘ExprBase::~ExprBase()’:
    copy.cc:11: warning: will never be executed
    copy.cc: In destructor ‘virtual ExprBase::~ExprBase()’:
    copy.cc:11: warning: will never be executed
    copy.cc: In destructor ‘virtual ExprBase::~ExprBase()’:
    copy.cc:11: warning: will never be executed
    copy.cc: In destructor ‘virtual Label::~Label()’:
    copy.cc:28: warning: will never be executed
    copy.cc: In member function ‘virtual ExprBase* Label::Copy() const’:
    copy.cc:45: warning: will never be executed
    copy.cc: In constructor ‘Label::Label(const TAG*)’:
    copy.cc:25: warning: will never be executed
    copy.cc:25: warning: will never be executed
    copy.cc: In constructor ‘ExprBase::ExprBase()’:
    copy.cc:9: warning: will never be executed

    Most of these errors go way when the Label::Copy() function is stashed in the class declaration. Why am I getting these errors?

    Second, what about the warning regarding "base class ‘class ExprBase’ should be explicitly initialized in the copy constructor"? How would I do that?

    Thanks for any tips!

    Brian

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I think the only warning worthwhile following is the one about explicitly initializing the base class. You'd do that using the initialization list:
    Code:
    Label( const Label& label ) throw() : ExprBase(label), tag_(label.tag_) {}
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Optics with a Twist skewray's Avatar
    Join Date
    Dec 2006
    Location
    Claremont, CA
    Posts
    31
    Fog is lifting here. The construction you give rings a bell.

    So the only remaining question is, what do all of these other warnings mean, and how do I know that I can ignore them in the future? This section of my code generates 500 lines of these. Just their sheer number is overwhelming. If I knew what caused them, perhaps I could write the code in a fashion more clear to the compiler. Or give up and turn off the offending warning flag, I suppose.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Which warning flag causes these messages?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Optics with a Twist skewray's Avatar
    Join Date
    Dec 2006
    Location
    Claremont, CA
    Posts
    31
    Instituting binary search, please wait... -Wunreachable-code. Here is the manual entry on it:

    -Wunreachable-code
    Warn if the compiler detects that code will never be executed.

    This option is intended to warn when the compiler detects that at least a whole line of source code will never be executed, because some condition is never satisfied or because it is after a procedure that never returns.

    It is possible for this option to produce a warning even though there are circumstances under which part of the affected line can be executed, so care should be taken when removing apparently-unreachable code.

    For instance, when a function is inlined, a warning may mean that the line is unreachable in only one inlined copy of the function.

    This option is not made part of -Wall because in a debugging version of a program there is often substantial code which checks correct functioning of the program and is, hopefully, unreachable because the program does work. Another common use of unreachable code is to provide behavior which is selectable at compile-time.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Do the warnings go away if you have code that actually uses the class?

    If no, I suggest just dropping the option. It can generate lots of spurious warnings, and there's nothing wrong with this code. (Except that Copy should pass *this instead of tag_.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Strange warning from MSVC 6
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 03-27-2006, 10:55 AM
  4. A warning of strange meaning
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2006, 02:26 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21