Don't forget about copy constructors and the copy assignment operator
Something that I've always found handy to do is to decide, right away, what the copy semantics of a particular class are. Generally it turns out that it makes no sense (or is a lot of work) to allow copying, in which case the standard approach is to define the copy constructor and assignment operator as private
.
An alternative approach and one that I think is a bit more self-documenting, is to inherit from boost::noncopyable
. This achieves the same effect and makes it immediately obvious that the class will not allow copying.
class MyClass : boost::noncopyable { };
The most correct way to use boost::noncopyable
is to inherit it privately, since inheriting it publicly could let people do really funny stuff like:
void stuff(const boost::noncopyable& obj);