I found an interesting fact about class's initilization list

This is a discussion on I found an interesting fact about class's initilization list within the C++ Programming forums, part of the General Programming Boards category; It's a good habit to initialize all members in a constructor's initialization list. Sometimes you have multiple constructors in a ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    Very convenient! Just want to share this with you
    it makes code less readable - so I doubt it is so usable
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    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, 12:21 PM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21