I can not find unambiguous information on the topic of resetting the class fields in the constructor. The notions of Ρ‚Ρ€ΠΈΠ²ΠΈΠ°Π»ΡŒΠ½Ρ‹ΠΉ and Π½Π΅Ρ‚Ρ€ΠΈΠ²ΠΈΠ°Π»ΡŒΠ½Ρ‹ΠΉ Ρ‚Ρ€ΠΈΠ²ΠΈΠ°Π»ΡŒΠ½Ρ‹ΠΉ especially confusing.

Suppose we have a class:

 class Object { int i; Type t; }; 

When can i and t remain uninitialized after constructing an object of class Object ?

Someone says that if a constructor is not explicitly described for Object , then the compiler will generate an implicit default constructor that will call the default constructors for each field. In this case, the default constructors will be called for the fields i and t . i will be 0.

However, if we define our constructor in which we do not explicitly call the constructors of the fields i and t , then when we call this constructor, the behavior will be unusual:

  • i will remain uninitialized;
  • t initialized by the default constructor t::t() .

I am completely confused:

 class Type { public: Type() : value{0} {} int value; }; class Trivial { public: int i; Type t; }; class NotTrivial { public: NotTrivial(){} int i; Type t; }; void main() { Trivial trivial; // ПолС i ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½ΠΎ 0. // ПолС t ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½ΠΎ. NotTrivial notTrivial; // ПолС i Π½Π΅ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½ΠΎ. // ПолС t ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½ΠΎ. } 
  • 2
    field i remains uninitialized in both cases. Because int is a type with a trivial constructor. In addition, the Trivial class actually has a non-trivial constructor, like NotTrivial . Well, there is also a fundamental error - by the value stored in the variable, it is impossible to judge whether this variable was initialized or not. - VTT
  • one
    These types of trivial constructor, I mean nothing to call. - VTT
  • one
    In order not to get confused, we must remember that in C ++ there is a rule - not to do any actions not ordered by the programmer. Field resetting is an action that takes time and resources. Therefore, no one even resets the integer fields in any classes until the programmer explicitly writes a constructor in which he resets the fields. - pepsicoca1
  • one
    If there is a constructor with parameters for a class field, then it is called. But you need to write his call and pass him the parameters. If there is a constructor without parameters for the class field, then it is called without parameters. If there is no constructor, then nothing is invoked. Consider that for built-in constructor types (zeroing or combining or somehow initializing variables of these types) there is no. - pepsicoca1
  • one
    To determine this, there is a trait std::is_trivially_constructible - VTT

1 answer 1

When does a constructor reset a class field?

Only in cases where

  • you yourself explicitly wrote a constructor for your class with your pens and you yourself registered in it the resetting of the fields of your class, or

  • You explicitly specified a null initializer for the non-static field in the class definition.

In C ++, there are other mechanisms for zeroing class fields, but they have nothing to do with constructors and work "bypassing" constructors. For example, such a mechanism is value-initialization for classes without a custom constructor.

In your class Object example, the constructor of this class is implicitly generated by the compiler and in no way does it reset the i field. However, value-initialization can be externally applied to this class and thereby cause the field i to be reset.

 { Object t; // `ti` содСрТит мусор Object u{}; // `ui` содСрТит 0 } 

Note again that in this example, the zeroing of ui not the result of the constructor’s work, but is the result of a completely extraneous process β€” value-initialization.