Prohibiting definition of a copy constructor in an inherited class

c++, copy-constructor, noncopyable

Solution

You need to delete the copy constructor of D. Right now you allow copy-construction of D's by not trying to copy-construct the base class. The following variants will not compile:

class E: noncopyable
{
};
E e, e2(e);

class F: noncopyable
{
public:
  F(const F &init): noncopyable(init)
  {}
};

Problem

I want to make an abstract base class non-copyable and force any classes that derive from it to be non-copyable. The below code uses Boost's noncopyable as defined in noncopyable.hpp yet still allows D, the derived class, to define a copy constructor. ``` class noncopyable { protected: noncopyable() {} ~noncopyable() {} private: // emphasize the following members are private noncopyable( const noncopyable& ); const noncopyable& operator=( const noncopyable& ); }; class D : noncopyable { public: D() { } D(const D&) { } }; int main() { D a; D b(a); return 0; } ``` This code compiles and runs (http://ideone.com/g4gGLm), when I expected it to throw a compile-time error about D's copy constructor. Maybe I have misinterpreted what this noncopyable class is meant to do. If so, is there any way to force derived classes to not define a copy constructor? (Answer can use C++11, but preferably not boost)

Original source