Before you start to mark this as duplicate I've already read this .But It doesn't answer my question. The linked question talks about C++98 & C++03 but my question is about defaulted constructor introduced by C++11.
Consider following program (See live demo here):
#include <iostream>
struct Test
{
int s;
float m;
Test(int a,float b) : s(a),m(b)
{ }
Test()=default;
}t;
int main()
{
std::cout<<t.s<<'\n';
std::cout<<t.m<<'\n';
}
My question is that is the defaulted constructor provided by compiler here always initializes built in types to by default 0 in C++11 & C++14 when they are class
& struct
members. Is this behavior guaranteed by C++11 standard?
As default constructor initializes the data members of class to 0.
The default constructor is auto-generated if there is no user-declared constructor (§12.1/5).
In both Java and C#, a "default constructor" refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. The default constructor implicitly calls the superclass's nullary constructor, then executes an empty body.
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .
Implicitly defined (by the compiler) default constructor of a class does not initialize members of built-in types. However, you have to keep in mind that in some cases the initialization of a instance of the class can be performed by other means.
This method is not only used in classes but also used with struct and union data types A Default Constructor is a constructor type in Classes that is called when class is defined with no arguments, or it is defined with an empty parameter list, or with default arguments provided for every parameter.
C++ allows even built-in type (primitive types) to have default constructors. The function style cast int() is analogous to casting 0 to required type. The program prints 0 on console. The initial content of the article triggered many discussions, given below is consolidation.
Built-in types are considered to have constructors”. The code snippet above mentioned int () is considered to be conceptually having constructor. However, there will not be any code generated to make an explicit constructor call. But when we observe assembly output, code will be generated to initialize the identifier using value semantics.
There are two separate questions built into this question.
What does = default
mean for the default constructor? From [class.ctor]:
A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used (3.2) to create an object of its class type (1.8) or when it is explicitly defaulted after its first declaration. The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer (12.6.2) and an empty compound-statement.
That is, Test() = default
is exactly equivalent to Test() { }
, which would default-initialize s
and m
, which sets them to some indeterminate value.
How are t.s
and t.m
initialized? Yes, this is a separate question from (1) because we're not just calling the default constructor here. From [basic.stc.static]:
All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration.
and from [basic.start.init]:
Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.
Thus t.s
and t.m
are guaranteed to be 0, even though if we default-constructed a local Test
, they would not be.
Test = default
will default initialize its members.
but for type as int
or float
, default initialization is different than value-initialization
so
Test t; // t.s and t.m have unitialized value
whereas
Test t{}; // t.s == 0 and t.m == 0.0f;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With