noncopyable

From Cppwiki

Jump to: navigation, search

Some classes are not intended to be copied. This occurs frequently enough in C++ that it is helpful to encapsulate this concept in its own class:

 class noncopyable {
 protected:
   // allow derived classes to be constructed
   noncopyable () { }
   // allow derived classes to be destroyed
   ~noncopyable () { }
 private:
   // do not allow derived classes to be copied
   noncopyable (const noncopyable &);
   // do not allow assignment to derived classes
   noncopyable &operator= (const noncopyable &);
 };

By deriving from noncopyable, you indicate that the class is not intended to be copied. Furthermore, when someone attempts to copy a noncopyable object, compilers will usually produce a helpful error message:

 error: `noncopyable::noncopyable(const noncopyable&)' is private

The Boost library provides an implementation of this concept as boost::noncopyable.

Personal tools