Boost logo

Boost :

From: Maxim Yegorushkin (e-maxim_at_[hidden])
Date: 2004-12-19 22:39:32

Dirk Gregorius wrote:


> I saw some code that declared the copy operations private in the base
> class.
> class Interface
> {
> public:
> virtual void f() = 0;
> private:
> Interface( const Interface& );
> Interface& operator=( const Interface& );
> }
> So in order to gain maximum style and correctness points I need to make
> both
> classes ( Interface and Implementation ) noncopyable?

Only a base class. This prevents a compiler from generating copy ctor and
assignment in its successors.

> Why is this possible?

Because a compiler generates copy ctor and assignment when nothing
prevents it.

> Does it make sense to dereference an interface?

It does make sense, since there can be functions taking an interface by
reference. But dereferencing and assigning it does not.

> Interface* p1 = make_interface();
> Interface* p2 = make_interface();
> *p1 = *p2;
> Or does your example only work with auto/smart pointers?

For plain pointers also.

Some say noncopyable interfaces may save from errors, though making them
noncopyable I've never encountered that kind of errors.

Maxim Yegorushkin

Boost list run by bdawes at, gregod at, cpdaniel at, john at