Thread: I found an interesting fact about class's initilization list

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    I found an interesting fact about class's initilization list

    It's a good habit to initialize all members in a constructor's initialization list.
    Sometimes you have multiple constructors in a class, and you have many many members to initialize, you may not want to list them all for each constructor, because if you want to change one member's initialized value, you have to go through all constructors' lists.

    Then I found you can use preprocessor:

    Code:
    #define INI x(0),y(0)
    
    class B{                                                                        
    private:                                                                        
        int x,y;                                                                    
    public:                                                                         
        B():INI{}                                                                   
        B(int z):INI{}                                                              
        ~B(){}                                                                      
        void print(){cout<<x<<"\t"<<y<<endl;}                                       
    };
    Very convenient! Just want to share this with you

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >    B(int z):INI{}
    What's the purpose of z?

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Very convenient! Just want to share this with you
    it makes code less readable - so I doubt it is so usable
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by meili100 View Post
    It's a good habit to initialize all members in a constructor's initialization list.
    Sometimes you have multiple constructors in a class, and you have many many members to initialize, you may not want to list them all for each constructor, because if you want to change one member's initialized value, you have to go through all constructors' lists.
    If you have overloaded constructors, they should be sufficiently different from each other than they don't all require the same initialization list. If you find yourself doing this, see if you can try to apply some default arguments first to cut down on the number of constructors. Classes with more than one constructor tend to be confusing to use. Of course, having a default constructor is always nice, if it makes sense.

    Some people recommend placing the common initialization in a private init() method, but this defeats the advantages of the initialization list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM