I have a warning from GCC that a base class should be initialized in the copy constructor. The problem is, I believe the original intention is its a constructor, but not a copy constructor. Here's what the simplified version looks like (I added the initializer Base()
in response to the warning):
class Base { ... };
class Derived : public Base
{
public:
Derived(Derived& d, int x = 0)
: Base(), m_b(d) { ... };
protected:
Base& m_b;
};
class Derived
provides the Base
interface, and it has a Base
member. The design is such that data can flow from a source to a sink, with arbitrary intermediate objects in between that can transform or process data (arbitrary but constrained by the interface).
The distinction between constructor and copy constructor is important. In the case of the constructor, Base
is being default initialized; and in the case of a copy constructor, Base
is being initialized with rhs
.
Is it sufficient to use the default constructed base Base
to give the compiler a hint that its a constructor, but not a copy constructor?
If not, how do I tell the compiler that the constructor is not a copy constructor? I'm happy to use a GCC extension, like an __attribute__
, to direct the compiler in the right direction.
(The other option, change the signature, cannot happen at the moment because it breaks versioning requirements on most major platforms, like Apple).
Here are the files in question:
Here is the compiler warning (the namespace has been removed for brevity):
g++ -DDEBUG -g2 -O2 -Wall -Wextra -fPIC -march=native -pipe -c asn.cpp
asn.cpp: In copy constructor ‘DERGeneralEncoder::DERGeneralEncoder(DERGeneralEncoder&, byte)’:
asn.cpp:497:1: warning: base class ‘class ByteQueue’ should be explicitly initialized in the copy constructor [-Wextra]
DERGeneralEncoder::DERGeneralEncoder(DERGeneralEncoder &outQueue, byte asnTag)
asn.cpp:497
is pointing to line 487 (the second constructor) in the unmodified online sources.
The system is Fedora 22, x86_64, fully patched, with GCC 5.1.1:
$ uname -a
Linux localhost.localdomain 4.1.6-201.fc22.x86_64 #1 SMP Fri Sep 4 17:49:24 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
$ lsb_module
bash: lsb_module: command not found...
$ g++ --version
g++ (GCC) 5.1.1 20150618 (Red Hat 5.1.1-4)
...