C++
InMyClass x;
) or allocated dynamically with no argument list (e.g.: new MyClass;
or new MyClass();
), the default constructor of MyClass
is used to initialize the object.
* When an array of objects is declared, e.g. MyClass x 0
; or allocated dynamically, e.g. new MyClass 0/code>. The default constructor of MyClass
is used to initialize all the elements.
* When a derived class constructor does not explicitly call the base class constructor in its initializer list, the default constructor for the base class is called.
* When a class constructor does not explicitly call the constructor of one of its object-valued fields in its initializer list, the default constructor for the field's class is called.
* In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly. E.g. vector (10);
initializes the vector with ten elements, which are filled with a default-constructed MyClass
object.
If a class has no explicitly defined constructors, the compiler will implicitly declare and define a default constructor for it. This implicitly defined default constructor is equivalent to an explicitly defined one with an empty body. For example:
class MyClass
;
int main()
If constructors are explicitly defined for a class, but they are all non-default, the compiler will not implicitly define a default constructor, leading to a situation where the class does not have a default constructor. This is the reason for a typical error, demonstrated by the following example.
class MyClass
;
MyClass::MyClass (int y)
int main()
Since neither the programmer nor the compiler has defined a default constructor, the creation of the objected pointed to by p
leads to an error.
On the other hand in C++11 a default constructor can be explicitly created:
class MyClass
;
Or explicitly inhibited:
class MyClass
;
Java and C#
In both Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
and C#, a "default constructor" refers to a nullary constructor
In computer programming, a nullary constructor is a constructor that takes no arguments. Also known as a 0-argument constructor, no-argument constructors or default constructor.
Object-oriented constructors
In object-oriented programming, a c ...
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. All fields are left at their initial value of 0 (integer types), 0.0 (floating-point types), false
(boolean
type), or null
(reference types). A programmer-defined constructor that takes no parameters is also called a default constructor in C#, but not in Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
.Using Constructors (C# Programming Guide)
/ref>
References
{{DEFAULTSORT:Default Constructor
Method (computer programming)