Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default value for C++ class members

People also ask

What is the default value in C?

value − Any value to initialize the variable. By default, it is zero.

What is the default value of structure members in C?

For variables of class types and other reference types, this default value is null . However, since structs are value types that cannot be null , the default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null .

What is the default value of class variable in C++?

Aside from the above cases, class members, once again, have no default values and will initially contain unpredictable garbage values. If static storage is zero-initialized would depend on the operating system you're running.

What is default initialization in C++?

Default member initializer (C++11) [edit] This is the initialization performed when an object is constructed with no initializer.


There are no differences between structs and classes in this regard in C++. They all are called just class types.

Members of class types have no default values in general case. In order to for a class member to get a deterministic value it has to be initialized, which can be done by

  • Default constructor of the member itself
  • Constructor initializer list of the enclosing class
  • Explicitly specified initializer for object of the enclosing class (that includes value-initialization and initialization with aggregate initializer).

Additionally, all objects with static storage duration are zero-initialized at the program startup.

Aside from the above cases, class members, once again, have no default values and will initially contain unpredictable garbage values.


Yeah, there is one. If you initialize an object with the default constructor and use parentheses then the POD members will be zero initialized:

someClass * p = new someClass();