- "default constructor" - 5 Updates
Wayne <wayne@nospam.invalid>: Jun 20 11:49PM -0400 On 6/18/2017 9:49 PM, Stefan Ram wrote: > 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" <ootiib@hot.ee>: Jun 21 01:12AM -0700 On Wednesday, 21 June 2017 06:49:44 UTC+3, Wayne wrote: > (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>.) 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 <nospam@thanks.invalid>: Jun 21 02:32PM > 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" <alf.p.steinbach+usenet@gmail.com>: Jun 21 06:07PM +0200 On 21-Jun-17 4:32 PM, Juha Nieminen wrote: > 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. 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" <arne@vajhoej.dk>: Jun 21 12:52PM -0400 On 6/18/2017 9:49 PM, Stefan Ram wrote: > "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 |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment