Inherit from the class from which the copy constructor was deleted.

I have a base class that contains only its own default constructor and the open remote copy constructor, and nothing more.

 class base { private: base() = default; public: base(const base&) = delete; }; 

If I try to inherit from base and create an instance of the derived class, as shown below, g ++ 4.8.2 does not compile my code, but VC ++ 2013 does.

 class derived : public base { private: derived() = default; }; derived x; 

So, is this a bug in g ++ or VC ++ 2013 just ignored something?

Here's the full 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; ^ 
+7
c ++ c ++ 11 visual-studio-2013 compiler-bug
source share
1 answer

You are reading the error incorrectly, it tells you that the default constructor for derived not available (this is private ), so you cannot use it to create an object of this type. Now doing this public at the derived level will not help, because the base constructor is also private and therefore cannot be used in the derived constructor.

Why do you want these constructors to be private ?

+5
source share

All Articles