Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: "default constructor"

37 views
Skip to first unread message

Wayne

unread,
Jun 20, 2017, 11:49:44 PM6/20/17
to
On 6/18/2017 9:49 PM, Stefan Ram wrote:
> Newsgroups: comp.lang.java.programmer,comp.lang.c++
>
> A default constructor is a constructor that misses something.
>
> In C++, it is a constructor that can be called with missing
> arguments (no arguments).
>
> C++ 12.1p4:
>
> A default constructor for a class X is a constructor of
> class X for which each parameter that is not a function
> parameter pack has a default argument (including the
> case of a constructor with no parameters).
>
> In Java, it is a constructor that is generated when an
> explicit constructor declaration is missing. The generated
> constructor just so happens to have no parameters, but
> an explicitly declared constructor with no parameters is
> not a default constructor, but replaces it:
>
> JLS8 13.4.12
>
> Adding one or more constructor declarations to the
> source code of such a class will prevent this default
> constructor from being implicitly declared, effectively
> deleting a constructor, unless one of the new
> constructors also has no parameters, thus replacing the
> default constructor.
>
> Summary (simplified): In C++, "default constructor" means
> "any constructor that does not require an argument".
> In Java, it means "the constructor that is generated if
> the user does not declare any constructor".
>
> Newsgroups: comp.lang.java.programmer,comp.lang.c++
>

Stefan is correct as usual, but for the sake of completeness:

In Java, a constructor that takes no arguments is often called
a "no-arg" constructor, while the constructor supplied by the
compiler is known as the "default" constructor. Java's default
constructor is a no-arg constructor.

In C++, a compiler-generated constructor is called "auto-generated.
Thus C++ has an auto-generated default constructor, while Java
has a default no-arg constructor. Both are similar concepts.

(Constructors in C++ can be more complicated then for Java, IMO.
See for example
<https://stackoverflow.com/questions/4943958/conditions-for-automatic-generation-of-default-copy-move-ctor-and-copy-move-assi#answer-38257488>.)

--
Wayne

Öö Tiib

unread,
Jun 21, 2017, 4:12:42 AM6/21/17
to
Yes, C++ (as legacy from C) automatically generates some code
for structs. That is done *very* carelessly even on the (easy to
detect) cases when the generated code is likely wrong. For example
programmers have manually to follow the rule of 3. The C++11 did
not repair that but at least added "=default" thingy. That added new
word "defaulted" to C++.
So I have more heard not "automatically generated" but "implicitly
defaulted" lately. Yes, it sounds bit like stutter about default
constructor ... "implicitly defaulted default constructor".

Juha Nieminen

unread,
Jun 21, 2017, 10:32:37 AM6/21/17
to
In comp.lang.c++ Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> In C++, it is a constructor that can be called with missing
> arguments (no arguments).

I have the impression that the word "default" is used in a somewhat
different meaning here.

Rather than meaning the same as "implicit constructor" (which is what
you are referring to), it means "the constructor that's called by
default when there is no explicit constructor call in the code
that instantiates the class" (or, in other words, when the class
is instantiated without any parameters).

In other words, when you write:

MyClass obj;

the default constructor is called because no other constructor
call was specified explicitly. It doesn't matter whether that
default constructor was implicitly created by the compiler, or
explicitly declared in the implementation of the class.

Alf P. Steinbach

unread,
Jun 21, 2017, 12:07:43 PM6/21/17
to
First, let me apologize for calling you ungrateful, in another thread. I
was angry. I stand by the rest (that your attacks were silly, and that
they really annoyed me, and so forth), because that was either objective
or reporting how I felt, but assigning motives and deducing
characteristics of people is Just Wrong. I did it out of anger but it
was still wrong of me. I had no basis for that, it was not an
evaluation, it was just very ugly name calling, lashing out. I'm sorry.

Now regarding what you write here, I agree with you in principle, that
this is a good way to think about it, a good conceptual model.

But it's worth being aware that this model, of the default constructor
being called, either a user-defined one or one generated by the
compiler, breaks down in a certain case, as illustrated below:


------------------------------------------------------------------------
#include <iostream>
#include <string>
using namespace std;

struct S
{
int score;
string name;
};

auto operator<<( ostream& stream, S const& o )
-> ostream&
{ return stream << "{" << o.name << ", " << o.score << "}"; }

auto main() -> int
{
S a; // Default initialization
S b{}; // Value initialization

cout << a << endl;
cout << b << endl;
}
------------------------------------------------------------------------


Results in Windows 10 using MinGW g++ 6.3.0 and Visual C++ 2017:


------------------------------------------------------------------------
[H:\forums\clc++\020 vaue init versus default init]
> g++ initializations.cpp

[H:\forums\clc++\020 vaue init versus default init]
> a
{, 65535}
{, 0}

[H:\forums\clc++\020 vaue init versus default init]
> cl initializations.cpp /Feb
initializations.cpp

[H:\forums\clc++\020 vaue init versus default init]
> b
{, 15726820}
{, 0}

[H:\forums\clc++\020 vaue init versus default init]
> _
------------------------------------------------------------------------


The funny `a.score` values stem from default initialization. It leaves
that member indeterminate. Value initialization zeroes it.

Now you wrote nothing about value initialization, but it's clear from
the difference that either value initialization uses a default
constructor that's not the one that default initialization uses, or one
or both of them doesn't actually call a constructor with a single
well-defined effect, or both compilers above are non-conforming.

In C++98 there was no value initialization so one had the indeterminate
value in both the `S a;` case and e.g. the `S* p = new S();` case.

Andrew Koenig then fixed this partially by proposing value
initialization, which was included in C++03. This was the only really
new thing in C++03, which otherwise was just Technical Corrigendum 1,
TC1, a bug-fix of C++98. But if we view it as a fix of a design level
bug (with the “design” the design of C++) it was also just a fix. ;-)


Cheers!, and again, sorry for that name calling,

- Alf

Arne Vajhøj

unread,
Jun 21, 2017, 12:53:14 PM6/21/17
to
On 6/18/2017 9:49 PM, Stefan Ram wrote:
> A default constructor is a constructor that misses something.
>
> In C++, it is a constructor that can be called with missing
> arguments (no arguments).
>
> C++ 12.1p4:
>
> A default constructor for a class X is a constructor of
> class X for which each parameter that is not a function
> parameter pack has a default argument (including the
> case of a constructor with no parameters).
>
> In Java, it is a constructor that is generated when an
> explicit constructor declaration is missing. The generated
> constructor just so happens to have no parameters, but
> an explicitly declared constructor with no parameters is
> not a default constructor, but replaces it:
>
> JLS8 13.4.12
>
> Adding one or more constructor declarations to the
> source code of such a class will prevent this default
> constructor from being implicitly declared, effectively
> deleting a constructor, unless one of the new
> constructors also has no parameters, thus replacing the
> default constructor.
>
> Summary (simplified): In C++, "default constructor" means
> "any constructor that does not require an argument".
> In Java, it means "the constructor that is generated if
> the user does not declare any constructor".

So?

C++ and Java are two different languages.

They also have somewhat different associated terminologies.

No surprise.

Arne

Ian Collins

unread,
Jun 22, 2017, 4:11:12 PM6/22/17
to
On 06/22/17 04:07 AM, Alf P. Steinbach wrote:
>
> auto operator<<( ostream& stream, S const& o )
> -> ostream&
> { return stream << "{" << o.name << ", " << o.score << "}"; }

That is so not idiomatic it made my eyes bleed!

--
Ian
0 new messages