Class initialization list

Just a simple question about C ++ programming style,

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

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

However, my question is, even if the default constructor is called, it is a good habit to always do this yourself or just add extra lines to the code

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

+1
c ++ initialization
Jan 07 2018-11-11T00:
source share
4 answers

It is a good idea to leave the default constructor. The constructors generated by the compiler are much more reliable and convenient than writing your own versions, and, among other things, it is a waste of time for writing code that the compiler could write for you. If you do not need any construction logic, then do not write the constructor.

However, int will not be initialized unless you do this, which is a serious reason for writing a constructor.

+2
Jan 07 '11 at 12:22
source share

You touch one of the sticky corners of C ++.

Initializing POD values ​​in objects is sticky and depends on several things.
Even I'm not sure that I can correct all the rules correctly, but I believe that @Steve Jessop once wrote an article on SO (although I can find it now).

But some examples:

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

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

Without the default constructor, it is clearly more complex:
Here the compiler will create a default constructor for you. But how the default compiler works depends on the situation. The default constructor created by the compiler can perform two different forms of initialization and which is used depending on the context:

  • Zero initialization (all POD members are reset)
  • Initialization of the value (all members of the POD remain 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(); } 
+4
Jan 07 2018-11-11T00:
source share

By default, int variables are not initialized with a value - you must do this yourself.

Therefore, when you do not set the member variable "value" for some value in the constructor, it remains uninitialized.

+1
Jan 07 '11 at 12:00
source share

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

+1
Jan 07 '11 at 12:08
source share



All Articles