C++11 scratches an itch that's long bothered me by allowing you to mark implicitly compiler defined methods as verboten with the "= delete" syntax. Wikipedia for more info.
class Foo
{
public:
Foo();
~Foo();
// No copy
Foo(Foo const &) = delete;
Foo& operator=(Foo const &) = delete;
};
Copy and assignment operators for classes that I do not expect to have copied or assigned are always a pain to mess with. It's a lot of boiler-plate code to make them private and then often there's member data that don't have a default constructor that require some hand-waving to make the compiler happy on a function you just want no one to ever call.
class Bar
{
public:
explicit Bar(UniqueResourceID id): m_data(id) { }
~Bar();
protected:
SomeHandle m_data; // no default constructor
// all this crap to keep from being able to copy. Do not use any of these!!
private:
Bar() { } // ERROR: m_data has no default constructor
static UniqueResourceID s_invalidID; // now I'm making the problem worse,
// because I don't actually need this
// for anything real, except to shut
// up some errors.
Bar(Bar const &o): m_data(s_invalidID) { }
Bar& operator =(Bar const &o): { return *this; }
};
Unfortunately, some of the compilers I have to use are not C++11 compilers and don't offer =delete. What's the best way of dealing with these? (Please tell me there's a better way than the second code snippet.)