I have class base
which only contains private default constructor and public deleted copy constructor, and nothing else.
class base {
private:
base() = default;
public:
base(const base&) = delete;
};
If I try to inherit from base
and create an instance of class derived
as below, g++ 4.8.2 doesn't compile my code but VC++ 2013 does.
class derived : public base {
private:
derived() = default;
};
derived x;
So, is it a bug in g++ or VC++ 2013 just ignored something?
Here's the complete code...
class base {
private:
base() = default;
public:
base(const base&) = delete;
};
class derived : public base {
private:
derived() = default;
};
derived x;
int main() {
}
... and g++ error message.
main.cpp:12:5: error: 'constexpr derived::derived()' is private
derived() = default;
^
main.cpp:15:9: error: within this context
derived x;
^
main.cpp: In constructor 'constexpr derived::derived()':
main.cpp:3:5: error: 'constexpr base::base()' is private
base() = default;
^
main.cpp:12:5: error: within this context
derived() = default;
^
main.cpp: At global scope:
main.cpp:15:9: note: synthesized method 'constexpr derived::derived()' first required here
derived x;
^
You are misreading the error, it is telling you that the default constructor for derived
is not accessible (is private
) so that you cannot use that to create an object of that type. Now making it public
at the derived
level won't help, since the base
constructor is also private
and thus cannot be used within the constructor of derived
.
Why do you want those constructors to be private
?
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