Wednesday, September 23, 2009

DISALLOW_COPY_AND_ASSIGN

Disable copy ctor and assignment operator using DISALLOW_COPY_AND_ASSIGN

From google coding standard..
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Copy_Constructors

Use copy constructors only when your code needs to copy a class; most do not need to be copied and so should use DISALLOW_COPY_AND_ASSIGN.

DISALLOW_COPY_AND_ASSIGN is a macro to disallow the copy constructor and operator= functions. This should be used in the private: declarations for a class



#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)


Usage :

class FunctionContext
{
public:
typedef SmartPointer Ref;

protected:
explicit FunctionContext(ServiceLocator* service_locator);

private:
DISALLOW_COPY_AND_ASSIGN(FunctionContext);
};



Qt Equivalent:

#define Q_DISABLE_COPY(Class) \
Class(const Class &); \
Class &operator=(const Class &);

No comments: