
Dirk Gregorius <dirk <at> dirkgregorius.de> writes:
Does inheriting from boost::noncopyable has the same effect like making the copy c'tor and the assignment operator private without implementing them? The authors of the smart_ptr library suggest the following for the use of the smart pointers with interfaces:
class Interface { public: virtual void f() = 0; virtual void g() = 0;
protected: ~Interface( void ) {}; // non-virtual }
// #1 class Implementation : public Interface { public: void f() { /* Implementation */ } void g() { /* Implementation */ }
private: // Not to be implemented Implementation( const Implementation& ); Implementation& operator=( const Implementation& ); }
Does inheriting from boost::noncopyable has the same effect:
// # 2 class Implementation : public Interface, /*public or private*/ boost::noncopyable { public: void f() { /* Implementation */ } void g() { /* Implementation */ } }
1.) Are #1 and #2 equivalent?
In a broad sense, yes. The problem is that many compilers don't do EBO (empty base optiimzation) in the presence of multiple inheritance, which might end up (in #2) with sizeof(Implementation) being greater than strictly needed.
2.) When should I derive public and when private from boost::noncopyable?
Given what I said, I think that deriving from boost::noncpyable must be avoided if multiple inheritance is present and you expct your class to be allocated on the stack. In all other cases, deriving from noncpyable is more expressive than the traditional idiom. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo