This code will not compile with gcc 4.7.0:
class Base
{
public:
Base(const Base&) = delete;
};
class Derived : Base
{
public:
Derived(int i) : m_i(i) {}
int m_i;
};
The error is:
c.cpp: In constructor `Derived::Derived(int)´:
c.cpp:10:24: error: no matching function for call to `Base::Base()´
c.cpp:10:24: note: candidate is:
c.cpp:4:2: note: Base::Base(const Base&) <deleted>
c.cpp:4:2: note: candidate expects 1 argument, 0 provided
In other words, the compiler does not generate a default constructor for the base class, and instead tries to call the deleted copy constructor as the only available overload.
Is that normal behavior?
C++11 §12.1/5 states:
A default constructor for a class
X
is a constructor of classX
that can be called without an argument. If there is no user-declared constructor for classX
, a constructor having no parameters is implicitly declared as defaulted (8.4).
Your Base(const Base&) = delete;
counts as a user-declared constructor, so it suppresses generation of the implicit default constructor. The workaround is of course to declare it:
Base() = default;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With