Ray's playground

 

Item 5: Know what functions C++ silently writes and calls(Effective C++)

  When is an empty class not an empty class? When C++ gets through with it. If you don't declare them yourself, compilers will declare their own versions of a copy constructor, a copy assignment operator, and a destructor. Furthermore, if you declare no constructors at all, compilers will also declare a default constructor for you. All these functions will be both public and inline.  

  If you want to support assignment in a class containing a reference member, you must define the copy assignment operator yourself. Compilers behave similarly for classes containing const members (such as objectValue in the modified class above). It's not legal to modify const members, so compilers are unsure how to treat them during an implicitly generated assignment function. Finally, compilers reject implicit copy assignment operators in derived classes that inherit from base classes declaring the copy assignment operator private. After all, compiler-generated copy assignment operators for derived classes are supposed to handle base class parts, too, but in doing so, they certainly can't invoke member functions the derived class has no right to call.

  Compilers may implicitly generate a class's default constructor, copy constructor, copy assignment operator, and destructor.

 

posted on 2009-11-24 21:54  Ray Z  阅读(288)  评论(0编辑  收藏  举报

导航