Inheritance diagram for NonCopyable:
Some classes should not support copying due to design purposes. A common practice is to declare copy constructor and assignment operator in the private section and not to define them.
class X { public: // ... private: // copying not allowed X& operator=(const X&); // (!) not defined X(const X&); // (!) not defined };
This practice is good, but too verbose. You should add these private declarations for each appropriated class. Moreover, it is not so clear for users. A better practice is to derive your class from the NonCopyable interface.
// default copying not allowed class Y: private omni::NonCopyable { public: // ... };
Class NonCopyable is used to protect derived classes from default copying (copy constructor and assignment operator which compiler generates by default). It is recommended to use private derivation.
Note that NonCopyable is not strict copying protection. Derived classes may define their own copy constructor and/or assignment operator.
NonCopyable | ( | ) | [inline, protected] |
Trivial constructor.
~NonCopyable | ( | ) | [inline, protected] |
Trivial destructor.