00001 #ifndef __NoClones_h__ 00002 #define __NoClones_h__ 00003 00004 /* Explanation of NoClones {{{ 00005 00006 C++ defines default copy constructors and assignment operators, which clone 00007 every member of a class. Stroustrup describes this behaviour as the result of 00008 "historical accident". For many non-trivial classes, especially those 00009 containing pointers, this default behaviour is too naive. In fact it often leads 00010 to heap corruption. 00011 00012 Sometimes it does not make any sense to copy an instance of a class. For 00013 example if it contains a unique file handle, or other lock on a system resource. 00014 Sometimes it is too much effort to write reliable replacement copy operations[1]. 00015 In either case a private copy constructor and a private assignment operator 00016 prevent accidental copying. [2] 00017 00018 Deriving a class from this class has the same preventative effect. It is also a 00019 bit neater and means that all the above explanation is centralised here. 00020 00021 IAM 09/2002 00022 00023 [1] An example of how it is very easy to make mistakes: 00024 http://www.mistybeach.com/articles/WhyIDontLikeCPlusPlusForLargeProjects.html 00025 If we don't need a copy feature it really isn't worth the hassle. 00026 [2] The C++ Programming Language. Stroustrup. 3rd edition. Section 10.4.6.3 00027 00028 }}} */ 00029 00030 class NoClones 00031 { 00032 protected: // Default constructor doesn't need to be public, but can't be private. 00033 NoClones() {}; // Lots of compiler complaints without default constructor. 00034 private: 00035 NoClones(const NoClones&); 00036 NoClones& operator=(const NoClones&); 00037 }; 00038 00039 #endif /* #ifndef __NoClones_h__ */
1.4.2