65.9K
CodeProject is changing. Read more.
Home

Making Your Classes Copy-safe

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Dec 12, 2012

CPOL
viewsIcon

10974

Prevent default copy constructor and assignment operator side effect

I decided in the middle of my project to make my project follow coding standard, and be more secure and encapsulated.  For me, a small macro definition changed everything just by disabling coping for all objects and preventing generation of default copy constructor and assignment operator.

#define DISABLE_COPY(CLASS_NAME) \
private: \  
CLASS_NAME ( const CLASS_NAME & );               // NOT IMPLEMENTED  \ 
CLASS_NAME & operator = ( const CLASS_NAME & );  // NOT IMPLEMENTED 

Inside your class, add this macro with the class name as parameter; just beside DECLARE_MESSAGE_MAP macro, for example.