Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable warning about explicitly initializing base constructor inside copy constructor of derived class

Tags:

c++

warnings

g++

I'm using g++ version 4.2.1 with -Wextra enabled. I'm including a header from a library, and I keep getting the following warning about a class in the library, which is enabled by -Wextra (I've replaced the class's actual name with BaseClass):

warning: base class ‘class BaseClass’ should be explicitly initialized in the copy constructor

My question is: how can I disable this warning? For example, -Wextra also enables -Wuninitialized, but I can override that simple by passing -Wno-uninitialized as a compiler flag. Is there anything similar for the warning about the copy constructor? I wasn't able to find the answer in the g++ manpages or in any other forum posts.

like image 401
user588303 Avatar asked Jan 26 '26 01:01

user588303


1 Answers

Given:

class BaseClass
{
public:
    BaseClass();
    BaseClass(const BaseClass&);
};

class DerivedClass : public BaseClass
{
public:
    DerivedClass(const DerivedClass&);
};

This copy constructor:

DerivedClass::DerivedClass(const DerivedClass& obj)
  // warning: no BaseClass initializer!
{
}

Really means the same as:

DerivedClass::DerivedClass(const DerivedClass& obj)
  // Default construct the base:
  : BaseClass()
{
}

You can put in a default-constructor initializer like the above if that's really what you mean, and the warning will go away. But the compiler is suggesting that you might actually want this instead:

DerivedClass::DerivedClass(const DerivedClass& obj)
  // Copy construct the base:
  : BaseClass(obj)
{
}
like image 75
aschepler Avatar answered Jan 28 '26 15:01

aschepler