Misplaced Pages

Default constructor: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 08:58, 7 February 2009 edit121.243.61.82 (talk)No edit summary← Previous edit Revision as of 21:36, 1 May 2009 edit undoRadagast83 (talk | contribs)18,709 edits No consensus for mergerNext edit →
Line 1: Line 1:
{{Mergefrom|Nullary constructor|date=October 2007}}

A '''default constructor''' is a ] that can be called with no arguments. A '''default constructor''' is a ] that can be called with no arguments.



Revision as of 21:36, 1 May 2009

A default constructor is a constructor that can be called with no arguments.

In C++ and Java, if (and only if) no constructors (default or non-default) are explicitly defined for a class, then the compiler will provide an implicit default constructor, which is equivalent to a default constructor which is blank. Therefore, a class is not guaranteed to have a default constructor (i.e. when the programmer explicitly defines only non-default constructors). Some programmers explicitly create a default constructor as a matter of habit, so they won't forget later, but this is not required.

In C++, arrays only have a default constructor, which constructs each of its elements using the default constructor for their type. It is an error to declare an array of a class type that does not have a default constructor.

In C++ and Java, if a derived class does not explicitly invoke a constructor of the base class (in C++ in the initializer list, in Java using super() in the first line), then the default constructor is implicitly invoked for the base class. If the base class does not have a default constructor, it is an error.

In C++, if an instance field of a class is not explicitly initialized in the initializer list, then the default constructor is invoked to initialize that field. If that type does not have a default constructor, it is an error.

Stub icon

This software article is a stub. You can help Misplaced Pages by expanding it.

Categories: