Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Making Your Classes Copy-safe

0.00/5 (No votes)
11 Dec 2012CPOL 11K  
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.

C++
#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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)