From: troplin@bluewin.ch   
      
   Dave Harris wrote:   
   > { Article reformatted; please limit your lines to 70 charaters -mod }   
   >   
   > roberts.noah@gmail.com (nroberts) wrote (abridged):   
   >> Say you were given a coding policy that stated, "Only initialize   
   >> members with non-trivial constructors and destructors in your   
   >> constructor's initialization list." Assuming they mean what they   
   >> are saying, and understand the difference between trivial and   
   >> non-trivial constructors, could there be a reasonable explanation   
   >> for this policy?   
   >   
   > They may be trying to say that:   
   >   
   > Class::Class() : a(0), b(0), c(0), d(0) {   
   > }   
   >   
   > would be better written as:   
   >   
   > Class::Class() {   
   > a = 0;   
   > b = 0;   
   > c = 0;   
   > d = 0;   
   > }   
   >   
   > The former may be more efficient if the members have code in their   
   > constructors, but if they are just ints, then the efficiency will be   
   > about the same. The latter version is arguably easier to read, write   
   > and edit.   
      
   I usually write it like that:   
      
   Class::Class()   
   : a(0)   
   , b(0)   
   , c(0)   
   , d(0)   
   { }   
      
   or:   
      
   Class::Class() :   
    a(0),   
    b(0),   
    c(0),   
    d(0)   
   { }   
      
   which is both quite readable and easy to edit IMO.   
      
   BTW, would it be legal to add an additional comma at the end of my second   
   example after the 'b(0)'?   
      
   In my experience, people that avoid initializer lists also tend to omit   
   meaningful constuctors and use a combination of default constuctor and   
   custom initialization method, leaving the object in an invalid state   
   between those two.   
   Like:   
      
   Class::Class()   
   /* implicit:   
   : a()   
   , b()   
   , c()   
   , d()   
   */   
   {   
    // a, b, c, d are in an invalid state   
    a.init(0);   
    b.create(0);   
    c.initialize(0);   
    d.init(0);   
   }   
      
   Tobi   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|