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 00:37, 13 January 2009 editDisturbedNerd999 (talk | contribs)758 edits Rv, not necessary to have both of these topics in same article← Previous edit Revision as of 08:58, 7 February 2009 edit undo121.243.61.82 (talk)No edit summaryNext edit →
Line 13: Line 13:
{{software-stub}} {{software-stub}}


] ]


] ]

Revision as of 08:58, 7 February 2009

It has been suggested that Nullary constructor be merged into this article. (Discuss) Proposed since October 2007.

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: