Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36687

Re: "default constructor"

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Wayne <wayne@nospam.invalid>
Newsgroups comp.lang.java.programmer, comp.lang.c++
Subject Re: "default constructor"
Date Tue, 20 Jun 2017 23:49:34 -0400
Organization A noiseless patient Spider
Lines 55
Message-ID <oicq5j$329$1@dont-email.me> (permalink)
References <default-constructor-20170619023834@ram.dialup.fu-berlin.de>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 7bit
Injection-Date Wed, 21 Jun 2017 03:45:55 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="d5661901190d0693c3fa62df4f229ecd"; logging-data="3145"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18qzthycysc/Kl/mlYYWHycAShPCO08Oc4="
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.0
In-Reply-To <default-constructor-20170619023834@ram.dialup.fu-berlin.de>
Content-Language en-US
Cancel-Lock sha1:RumTc4ObN7z3GDk1cuLWB+TA4wo=
Xref csiph.com comp.lang.java.programmer:36687 comp.lang.c++:51495

Cross-posted to 2 groups.

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | Next | Find similar | Unroll thread


Thread

Re: "default constructor" Wayne <wayne@nospam.invalid> - 2017-06-20 23:49 -0400

csiph-web