Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ default constructor

If we say that the default constructor is that constructor without parameters, can we also say the the constructor created by the compiler is also a default constructor?

Thanks.

like image 827
Simplicity Avatar asked Jan 29 '11 11:01

Simplicity


People also ask

What is the default value of default constructor?

Default constructors do not take any parameters. If a default constructor is not provided by the programmer explicitly, then the compiler provides a implicit default constructor. In that case, the default values of the variables are 0. A program that demonstrates default constructors is given as follows.

What is the default constructor of a struct in Java?

Constructors for struct types resemble class constructors, but structs cannot contain an explicit default constructor because one is provided automatically by the compiler. This constructor initializes each field in the struct to the default values.

What are the different types of constructors in C++?

The two main types of constructors are default constructors and parameterized constructors. Default constructors do not take any parameters. If a default constructor is not provided by the programmer explicitly, then the compiler provides a implicit default constructor. In that case, the default values of the variables are 0.

What is a final constructor in C++?

final (C++11) A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is DefaultConstructible . Contents.


1 Answers

A default constructor is one that can be called without arguments.

C++98 §12.1/5:

A default constructor for a class X is a constructor of X that can be called without an argument. If there is no user-declared constructor for class X, a default constructor is implicitly declared.

like image 186
Cheers and hth. - Alf Avatar answered Oct 03 '22 21:10

Cheers and hth. - Alf