Default constructor: Difference between revisions
m Bot: Migrating 1 interwiki links, now provided by Wikidata on d:q4231567 |
m change source to syntaxhighlight |
||
(35 intermediate revisions by 25 users not shown) | |||
Line 1: | Line 1: | ||
In computer [[programming languages]] the term |
In computer [[programming languages]], the term '''default constructor''' can refer to a [[constructor (computer science)|constructor]] that is automatically generated by the compiler in the absence of any programmer-defined constructors (e.g. in Java), and is usually a [[nullary constructor]]. In other languages (e.g. in C++) it is a constructor that can be called without having to provide any arguments, irrespective of whether the constructor is auto-generated or user-defined. Note that a constructor with formal [[Parameter (computer science)|parameter]]s can still be called without arguments if [[default argument]]s were provided in the constructor's definition. |
||
== C++ == |
== C++ == |
||
In [[C++]], the standard describes the default constructor for a class as a [[constructor (computer science)|constructor]] that can be called with no arguments (this includes a constructor whose parameters all have default arguments).<ref>C++ standard, ISO/IEC 14882:1998, 12.1.5<br />C++ standard, ISO/IEC 14882:2003, 12.1.5</ref> For example: |
In [[C++]], the standard describes the default constructor for a class as a [[constructor (computer science)|constructor]] that can be called with no arguments (this includes a constructor whose parameters all have default arguments).<ref>C++ standard, ISO/IEC 14882:1998, 12.1.5<br />C++ standard, ISO/IEC 14882:2003, 12.1.5</ref> For example: |
||
< |
<syntaxhighlight lang="cpp"> |
||
class MyClass |
class MyClass |
||
{ |
{ |
||
public: |
|||
MyClass(); |
MyClass(); // constructor declared |
||
private: |
|||
int x; |
int x; |
||
}; |
}; |
||
MyClass |
MyClass::MyClass() : x(100) // constructor defined |
||
{ |
{ |
||
⚫ | |||
} |
} |
||
int main() |
int main() |
||
{ |
{ |
||
MyClass |
MyClass m; // at runtime, object m is created, and the default constructor is called |
||
} |
|||
} // => default constructor called automatically |
|||
</syntaxhighlight> |
|||
</source> |
|||
When allocating memory dynamically, the constructor may be called by adding parenthesis after the class name. In a sense, this is an explicit call to the constructor: |
|||
< |
<syntaxhighlight lang="cpp"> |
||
int main() |
int main() |
||
{ |
{ |
||
MyClass * pointer = new MyClass(); // object created |
MyClass * pointer = new MyClass(); // at runtime, an object is created, and the |
||
// default constructor is called |
|||
} |
|||
</source> |
|||
</syntaxhighlight> |
|||
⚫ | |||
<source lang='cpp'> |
|||
⚫ | |||
</source> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
In the above circumstances, it is an error if the class does not have a default constructor. |
|||
⚫ | |||
class MyClass |
|||
{ |
|||
public: |
|||
MyClass (int i = 0, std::string s = ""); // constructor declared |
|||
private: |
|||
The compiler will implicitly define a default constructor if no constructors are explicitly defined for a class. This implicitly declared default constructor is equivalent to a default constructor defined with a blank body. For example: |
|||
int x; |
|||
⚫ | |||
int y; |
|||
std::string z; |
|||
}; |
|||
MyClass::MyClass(int i, std::string s) // constructor defined |
|||
{ |
|||
⚫ | |||
y = i; |
|||
z = s; |
|||
} |
|||
</syntaxhighlight> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | * In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly. E.g. <code>vector<nowiki><MyClass></nowiki>(10);</code> initializes the vector with ten elements, which are filled with a default-constructed <code>MyClass</code> 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:<ref>Computer Science A Structured Approach Using C++ by Behrouz A. Forouzan and Richard F. Gilberg</ref> |
||
<syntaxhighlight lang="cpp"> |
|||
class MyClass |
class MyClass |
||
{ |
{ |
||
int x; |
int x; // no constructor, so the compiler produces an (implicit) default constructor |
||
}; |
|||
}; // => the compiler produces an (implicit) default constructor |
|||
int main() |
int main() |
||
{ |
{ |
||
MyClass |
MyClass m; // no error at runtime: the (implicit) default constructor is called |
||
} |
} |
||
</syntaxhighlight> |
|||
⚫ | |||
If |
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. |
||
< |
<syntaxhighlight lang="cpp"> |
||
class MyClass |
class MyClass |
||
{ |
{ |
||
public: |
|||
MyClass (int y); |
MyClass (int y); // declaration a non-default constructor |
||
private: |
|||
int x; |
int x; |
||
}; |
}; |
||
MyClass :: MyClass (int y) |
|||
⚫ | |||
{ |
{ |
||
x = y; |
x = y; |
||
} |
} |
||
int main() |
int main() |
||
{ |
{ |
||
MyClass |
MyClass m(100); // the non-default constructor is called |
||
MyClass * |
MyClass * p; // for pointer declarations, the compiler does not need to know about constructors |
||
p = new MyClass(); // error at compilation: no default constructor |
|||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</source> |
|||
⚫ | |||
⚫ | |||
On the other hand in C++11 a default constructor can be explicitly created: |
|||
<syntaxhighlight lang="cpp"> |
|||
class MyClass |
|||
{ |
|||
public: |
|||
MyClass () = default; // force generation of a default constructor |
|||
}; |
|||
</syntaxhighlight> |
|||
Or explicitly inhibited: |
|||
<syntaxhighlight lang="cpp"> |
|||
class MyClass |
|||
{ |
|||
public: |
|||
MyClass () = delete; // prevent generation of default constructor |
|||
}; |
|||
</syntaxhighlight> |
|||
== Java and C# == |
== Java and C# == |
||
In both [[Java (programming language)|Java]] and [[C Sharp (programming language)|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 |
In both [[Java (programming language)|Java]] and [[C Sharp (programming language)|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. All fields are left at their initial value of 0 (integer types), 0.0 (floating-point types), <code>false</code> (<code>boolean</code> type), or <code>null</code> (reference types). A programmer-defined constructor that takes no parameters is also called a default constructor in [[C Sharp (programming language)|C#]], but not in [[Java (programming language)|Java]].<ref>[http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.8.9 Java Language Specification, 3rd edition, section 8.8.9], "Default Constructor".</ref><ref>[http://msdn.microsoft.com/en-us/library/ms173115(v=vs.80).aspx#MSDN Using Constructors (C# Programming Guide)]</ref> |
||
== References == |
== References == |
Latest revision as of 15:57, 31 January 2021
In computer programming languages, the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors (e.g. in Java), and is usually a nullary constructor. In other languages (e.g. in C++) it is a constructor that can be called without having to provide any arguments, irrespective of whether the constructor is auto-generated or user-defined. Note that a constructor with formal parameters can still be called without arguments if default arguments were provided in the constructor's definition.
C++
[edit]In C++, the standard describes the default constructor for a class as a constructor that can be called with no arguments (this includes a constructor whose parameters all have default arguments).[1] For example:
class MyClass
{
public:
MyClass(); // constructor declared
private:
int x;
};
MyClass::MyClass() : x(100) // constructor defined
{
}
int main()
{
MyClass m; // at runtime, object m is created, and the default constructor is called
}
When allocating memory dynamically, the constructor may be called by adding parenthesis after the class name. In a sense, this is an explicit call to the constructor:
int main()
{
MyClass * pointer = new MyClass(); // at runtime, an object is created, and the
// default constructor is called
}
If the constructor does have one or more parameters, but they all have default values, then it is still a default constructor. Remember that each class can have at most one default constructor, either one without parameters, or one whose all parameters have default values, such as in this case:
class MyClass
{
public:
MyClass (int i = 0, std::string s = ""); // constructor declared
private:
int x;
int y;
std::string z;
};
MyClass::MyClass(int i, std::string s) // constructor defined
{
x = 100;
y = i;
z = s;
}
In C++, default constructors are significant because they are automatically invoked in certain circumstances; and therefore, in these circumstances, it is an error for a class to not have a default constructor:
- When an object value is declared with no argument list (e.g.:
MyClass x;
) or allocated dynamically with no argument list (e.g.:new MyClass;
ornew MyClass();
), the default constructor ofMyClass
is used to initialize the object. - When an array of objects is declared, e.g.
MyClass x[10];
; or allocated dynamically, e.g.new MyClass [10]
. The default constructor ofMyClass
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<MyClass>(10);
initializes the vector with ten elements, which are filled with a default-constructedMyClass
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:[2]
class MyClass
{
int x; // no constructor, so the compiler produces an (implicit) default constructor
};
int main()
{
MyClass m; // no error at runtime: the (implicit) default constructor is called
}
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
{
public:
MyClass (int y); // declaration a non-default constructor
private:
int x;
};
MyClass::MyClass (int y)
{
x = y;
}
int main()
{
MyClass m(100); // the non-default constructor is called
MyClass * p; // for pointer declarations, the compiler does not need to know about constructors
p = new MyClass(); // error at compilation: no default constructor
return 0;
}
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.[3]
On the other hand in C++11 a default constructor can be explicitly created:
class MyClass
{
public:
MyClass () = default; // force generation of a default constructor
};
Or explicitly inhibited:
class MyClass
{
public:
MyClass () = delete; // prevent generation of default constructor
};
Java and C#
[edit]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. 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.[4][5]
References
[edit]- ^ C++ standard, ISO/IEC 14882:1998, 12.1.5
C++ standard, ISO/IEC 14882:2003, 12.1.5 - ^ Computer Science A Structured Approach Using C++ by Behrouz A. Forouzan and Richard F. Gilberg
- ^ Computer Science A Structured Approach Using C++ by Behrouz A. Forouzan and Richard F. Gilberg
- ^ Java Language Specification, 3rd edition, section 8.8.9, "Default Constructor".
- ^ Using Constructors (C# Programming Guide)