Thread: consideration for intilialization list

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    consideration for intilialization list

    what are some of the considerations when putting member variables in a initializer list......are there some instances when it makes no performace increase or sense to put it in there? I know const members variables have to be initialized in there.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    basically when does it make sense to put things in the initializer list of the constructor?

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I only do it when they have to be done there. As in the case of const variables and in the case where a member object's constructor requires a parameter. I just don't like how it looks when you have a huge list. But that's just me..... and I rule.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by noob2c
    basically when does it make sense to put things in the initializer list of the constructor?
    Initialize your variables there whenever you can. If you just assign to them in the body of the constructor, the variable has to be constructed by the default constructor, then cleared and reassigned by the assignment operator. Doing it in the initializer list just invokes the copy constructor (or whatever constructor you wrote for it).

    are there some instances when it makes no performace increase or sense to put it in there?
    Not really. If you have a derived class, you can't initialize the base object's objects with it. So you may want to reassign the variables there.

    NOTE: most of the time you can avoid that by sending initialization data to the parent class for it to initialize objects. ie:
    Code:
    class B {
    public:
      B();
      B(int x) : value(x) {}
    private:
      int value;
    };
    class A : public B {
    public:
      A();
      A(int x) : B(x) {}
    };
    NOTE #2: All classes are constructed from Base to Derived. The Derived just shows what Base constructor to call; it doesn't call it itself.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    ygfperson has hit it on the head; for variables that are automatically initialized (i.e. any non-primitive type), initializing in the list calls only the constructor, initializing in the body requires both constructor and copy operator are called.

    There is another reason, as well. When the constructor exists completely as an initialization list (and the body is empty), the compiler may be able to optimize it better.

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. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM