Changes between Version 16 and Version 17 of CodingGuidelines


Ignore:
Timestamp:
Mar 29, 2012, 11:14:24 AM (13 years ago)
Author:
FrederikHeber
Comment:

added sections on copy and assignment operators

Legend:

Unmodified
Added
Removed
Modified
  • CodingGuidelines

    v16 v17  
    122122
    123123== Some hints on good code vs. bad code == #code-good-bad
     124
     125Note that many of these hints are taken from the book of [Meyers, Effective C++].
    124126
    125127=== end of stream checking === #code-good-bad_eof
     
    273275 * '''Const variables''' simply declare a (even global) const variable to contain the value.
    274276
     277=== Copy and Assigment constructors are potentially dangerous === #code-good-bad-copy-assignment
     278
     279Copy constructors such as
     280{{{
     281class A {
     282public:
     283    A();
     284    A(class A&);
     285    ~A();
     286
     287   A& operator = (const class A&);
     288}
     289}}}
     290are dangerous to implement by hand and not leave it to the compiler, which generally does a good job. This is especially the case for derived classes with self-implemented copy constructors, where the copying of base class elements is often forgotten. Or when classes are extended by new member attributes and these are not included in the copy constructor as well.
     291
     292See to it that unnecessary self-implemented copy constructors are removed and if ... implement a Init() function that both copy constructors use.
     293
     294=== Check for self-assignment === #code-good-bad-self-assignment
     295
     296If you have to implement a copy/assignment operator, always check for self-assignment!
     297{{{
     298SomeClass& SomeClass&::operator=(const SomeClass &instance)
     299{
     300 if (*this  != instance) {
     301   ...
     302 }
     303}
     304}}}