Misplaced Pages

Default constructor

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.

This is an old revision of this page, as edited by 121.243.61.82 (talk) at 08:58, 7 February 2009. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Revision as of 08:58, 7 February 2009 by 121.243.61.82 (talk)(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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: