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