Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Talk:Default constructor

Page contents not supported in other languages.
Add topic
From Wikipedia, the free encyclopedia
Latest comment: 2 months ago by ~2026-25821-88 in topic C++ value- vs default-initialization

Generalize

[edit]

Needs to be generalized. Java also has default constructors. And what about VB or C#? --Uncle Ed 17:02, 16 November 2006 (UTC)Reply

Object They are clearly two different things. A default constructor is a type of nullary constructor, but a programmer could add a nullary constructor to the program him/herself and it wouldn't be a default constructor. —Preceding unsigned comment added by DisturbedNerd999 (talkcontribs)

C++

[edit]

In the case of C++, a default constructor can have parameters, so long as all the parameters have themselves default values. These are plainly not nullary constructors. —SlamDiegoT 21:52, 18 July 2009 (UTC)Reply

Default constructor != Compiler-generated constructor

[edit]

The article confuses a default constructor with a compiler-generated constructor. A default constructor is not necessarily generated by the compiler. If the programmer writes a constructor without arguments (or with arguments that all have default values), it is still a default constructor, but at the same time it is a user-defined constructor. The notions of user-defined vs compiler-generated constructors are orthogonal to default and non-default constructors.  Preceding unsigned comment added by Korbateck-delta (talkcontribs) 09:46, 4 April 2014 (UTC)Reply

C++ value- vs default-initialization

[edit]

The article claims that in C++, an implicitly-defined default constructor (when no other constructors are present) is equivalent to one with an empty body. This is technically correct and does correspond to the standard wording (see below), but in practice it's not entirely true.

This is because of the distinction between default-initialization and value-initialization:

  • Default-initialization happens when an object is constructed without any initializer at all – e.g. Foo foo;, new Foo. For scalar types like integers or pointers, this leaves them uninitialized.
  • Value-initialization happens when an object is constructed with an explicitly empty argument list – e.g. Foo foo{};, new Foo{}. For scalar types like integers or pointers, this zero-initializes them.

For classes without user-defined constructors (where a default constructor is implicitly declared and defined), or where a default constructor is explicitly defaulted, the distinction is propagated to all members – that is, every such member is either uninitialized or zero-initialized, depending on which language construct/syntax was used to initialize the object.

For classes that have a (user-defined) default constructor, both of these call that default constructor. This includes the case that the constructor has no member initializers and its body is empty. For such a constructor, all members of scalar type are always left uninitialized, regardless of which syntax was used.

Example (https://godbolt.org/z/bedcqK39o):

#include <iostream>

struct Implicit { int m; };
struct EmptyBody { int m; EmptyBody() {} };

int main() {
    Implicit implicit{};
    EmptyBody empty{};
    std::cout << implicit.m << '\n'; // prints "0"
    std::cout << empty.m << '\n'; // undefined (C++26: erroneous) behavior:
                                  // m is uninitialized
}

Further reading: https://en.cppreference.com/cpp/language/default_constructor#Implicitly-defined_default_constructor

In the standard (C++23 final draft (N4950)), the whole thing is specified a little differently, but the effect is the same: section 11.4.5.2 clause 4 specifies that a default constructor that is implicitly-defined or defaulted is exactly equivalent to a user-provided one with no initializers and empty body. However, section 9.4.1 clause 9.1 distinguishes the two cases again, saying that for value-initialization, if the default constructor is implicitly-defined, the object is zero-initialized before default-initializing (thus calling the default constructor).

~2026-25821-88 (talk) 09:45, 28 April 2026 (UTC), edited 08:59, 29 April 2026 (UTC)Reply