0

Possible Duplicate:
Benefits of Initialization lists

I've been learning C++ for the past few days now and I'm seeing two formats where I can't determine advantages/disadvantages of the two. Hope someone can help me here.

The first one has variables being initialized with var(value)

class Foo
{
    public:
        Foo(): itsVar1(2), itsVar2(345){}

    private:
        int itsVar1;
        int itsVar2;      
};

The second is initialized with the assignment operator var = value.

class Foo
{
    public:
        Foo()
        {
            itsVar1 = 2;
            itsVar2 = 345;
        }

    private:
        int itsVar1;
        int itsVar2;
}; 

Is there an advantage to one over the other? Is it personal preference?
The first style(?) looks more confusing to me. It looks like you're calling a method and passing in that value. Looks very implicit; whereas, the second is very explicit and as someone coming from Python "explicit is better than implicit" I prefer the second method. What am I missing?

Community
  • 1
  • 1
Jeff
  • 6,932
  • 7
  • 42
  • 72
  • 2
    The ":" syntax is a C++ "initialization list". There are some circumstances where you *must* use them. In other cases (like your example) you can choose between a constructor body and an initialization list. See the link James McNellis cited, or Google for "C++ constructor vs initialization list" for more details. Here's one more link: http://forums.codeguru.com/showthread.php?t=464084 – paulsm4 Jun 06 '12 at 23:27

3 Answers3

3

The former notation will initialize members before constructor call. Nothing will be executed inside the constructor until what has been said is initialized. If there is inheritance, constructor for the parent class(es) will be called here before current constructor call.

As well, explicit and implicit declarations for constructors are varied in c++ from python I think. When your constructor has only one argument which is not the type of the class itself( the constructor is not a copy constructor ), it can be used for type conversion. When you put the explicit before the name of constructor, you tell compiler which do not convert anything without my explicit permission.

Kamran Amini
  • 1,062
  • 8
  • 14
2

Always use initializer lists (the first style) whenever you can. The major good reasons for doing so are:

  1. Not using initializer lists makes it harder to guarantee exception safety. Using RAII classes for your class member variables, along with initializer lists, makes it trivial to guarantee that your constructors won't leak resources when faced with exceptions thrown by the constructors of the member variables.

  2. Assignments are no better performing than initializer lists. If the member variable types are nontrivial, the assignments will perform worse in fact. With initializers, member variables will be constructed in place and there will be no default construction happening. Using assignments lets all the member variables become default constructed first.

  3. Initializer lists are the only way to assign values to reference and const member variables.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

When a constructor is declared as

 Foo(): itsVar1(2), itsVar2(345){}

it means that the variables "itsVar1" "and itsVar2" are initialized at the time of creation of its local variable members (i.e itsVar and itsVar2) . This method of initializing is known as initialization lists. In case of User defined types, such an initialization would invoke copy constructor of the User-defined Type. More info at: http://en.wikipedia.org/wiki/Initialization_(programming)#Initializer_list

When a constructor is defined as

  Foo()
    {
        itsVar1 = 2;
        itsVar2 = 345;
    }

the constructor creates memory and does not initialize the member variables, itsVar1 and itsVar2. They are assigned a value once the members are created and the control flows into the body of the constructor.

The advantage of using initializer list is that the member variable are not invoked twice (once during creation and another during assignment of a value). Creation and initialization of the member variable happen at the same time. If there are a number of variables in your initializer list, you would get a slight performance benefit at the time that the constructor is invoked than the other method.

Vishnu Pedireddi
  • 2,142
  • 4
  • 25
  • 34