hidayat
hidayat

Reputation: 9763

class initialization list

Just a simple question about c++ coding style,

for example, all member variables of a class will be called with the default constructor in the initialization list if we don't do anything else. B default constructor will be called and value will be set to 0, int();

class A
{
   A();
private:
   B b;
   int value;
}

However my question is, even do the default constructor will be called is it a good habit to always do it yourself or does it only add extra lines to the code

A::A() : b(), value() {}

Upvotes: 1

Views: 1493

Answers (4)

Loki Astari
Loki Astari

Reputation: 264729

You are touching on one of the sticky corners of C++.

The initialization of POD values in objects is sticky and depends on a few things.
Even I am not sure I can get all the rules correct but I believe @Steve Jessop once wrote an article about here on SO (though I can currently find it).

But some examples:

This class will always be initialized b == false and value = 0.

class A
{
   A() : b(), value() {}
   B b;
   int value;
};

Without an explicit default constructor it is more complex:
Here the compiler will generate a default constructor for you. But how the compiler generated default constructor works depends on the situation. The compiler generated default constructor can do two different forms of initialization and which is used depends on context:

  1. Zero Initialization (All POD members are zero'ed out)
  2. Value Initialization (All POD members are left undefined)

Example:

class B
{
   B b;
   int value;
};

// Variables of static storage duration (notably globals)
// Will be zero initialized and thus b == false and value = 0

B   global; // Initialized

int main()
{
    // Object of automatic storage duration or dynamic storage duration
    // These will depend on how they are declared.

    // Value Initialization (POD re-mains undefined)
    B    bo1;           // b/value undefined
    B*   bp1 = new B;   // b.balue undefined

    // Zero Initialization
    B    bo2 = B();     // b = false, value = 0
    B*   bp2 = new B(); // b = false, value = 0

    // Note: The most obvious syntax for zero initializing a local object
    //        does not work as it is actually interpreted as a forward
    //        declaration of a function;
    B   bo3(); 
}

Upvotes: 4

Puppy
Puppy

Reputation: 147056

It's a good idea to leave the default constructor out. The compiler-generated constructors are much more reliable and maintainable than writing your own versions, and apart from anything else, it's a flat out waste of time to write code the compiler could write for you. If you don't need any construction logic, then don't write a constructor.

However, the int will not be initialized unless you do it, which is a sucky reason to have to write a constructor.

Upvotes: 2

fredoverflow
fredoverflow

Reputation: 263360

The default constructor will only be called for class types with default constructors, not for primitives.

Upvotes: 1

Nubok
Nubok

Reputation: 3669

By default int variables are not initialized with a value - you have to do it yourself.

So when you don't set the member variable "value" to some value in the constructor it is left uninitialized.

Upvotes: 1

Related Questions