Questions tagged [default-constructor]

A default constructor is a parameterless constructor, often generated by the compiler.

817 questions
244
votes
3 answers

How is "=default" different from "{}" for default constructor and destructor?

I originally posted this as a question only about destructors, but now I'm adding consideration of the default constructor. Here's the original question: If I want to give my class a destructor that is virtual, but is otherwise the same as what…
227
votes
9 answers

Default constructor with empty brackets

Is there any good reason that an empty set of round brackets (parentheses) isn't valid for calling the default constructor in C++? MyObject object; // ok - default ctor MyObject object(blah); // ok MyObject object(); // error I seem to type…
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
202
votes
7 answers

Does the default constructor initialize built-in types?

Does the default constructor (created by the compiler) initialize built-in-types?
189
votes
13 answers

Java default constructor

What exactly is a default constructor — can you tell me which one of the following is a default constructor and what differentiates it from any other constructor? public Module() { this.name = ""; this.credits = 0; this.hours = 0; } public…
antanis
  • 1,909
  • 2
  • 12
  • 4
187
votes
14 answers

Kotlin with JPA: default constructor hell

As JPA requires, @Entity classes should have a default (non-arg) constructor to instantiate the objects when retrieving them from the database. In Kotlin, properties are very convenient to declare within the primary constructor, as in the following…
hotkey
  • 140,743
  • 39
  • 371
  • 326
168
votes
11 answers

Why does the default parameterless constructor go away when you create one with parameters

In C#, C++ and Java, when you create a constructor taking parameters, the default parameterless one goes away. I have always just accepted this fact, but now I've started wondering why. What is the reason for this behavior? Is it just a "safety…
olagjo
  • 2,125
  • 2
  • 14
  • 16
153
votes
3 answers

Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?

I want to refresh my memory on the conditions under which a compiler typically auto generates a default constructor, copy constructor and assignment operator. I recollect there were some rules, but I don't remember, and also can't find a reputable…
145
votes
5 answers

Default initialization of std::array?

With C++11 std::array, do I have the guarantee that the syntax std::array x; will default-initialize all the elements of the array ? EDIT: if not, is there a syntax that will work on all arrays (including zero-sized arrays) to initialize all…
Vincent
  • 57,703
  • 61
  • 205
  • 388
137
votes
12 answers

How to disable warning on Sonar: Hide Utility Class Constructor?

I'm getting this warning on Sonar: Hide Utility Class Constructor: Utility classes should not have a public or default constructor My class: public class FilePathHelper { private static String resourcesPath; public static String…
105
votes
4 answers

Creating instance of type without default constructor in C# using reflection

Take the following class as an example: class Sometype { int someValue; public Sometype(int someValue) { this.someValue = someValue; } } I then want to create an instance of this type using reflection: Type t =…
Aistina
  • 12,435
  • 13
  • 69
  • 89
97
votes
3 answers

When is a private constructor not a private constructor?

Let's say I have a type and I want to make its default constructor private. I write the following: class C { C() = default; }; int main() { C c; // error: C::C() is private within this context (g++) // error:…
Barry
  • 286,269
  • 29
  • 621
  • 977
82
votes
8 answers

Should we always include a default constructor in the class?

I have been asked this question by a colleague that should we always include a default constructor in a class? If so, why? If no, why not? Example public class Foo { Foo() { } Foo(int x, int y) { ... } } I am also interested…
Moon
  • 33,439
  • 20
  • 81
  • 132
72
votes
7 answers

Why do C++ objects have a default destructor?

When I don't declare a constructor for example, the compiler will provide me with a default constructor that will have no arguments and no definition (empty body), and thus, will take no action. So, if I'm finished with an object for example,…
Simplicity
  • 47,404
  • 98
  • 256
  • 385
71
votes
3 answers

Why won't this compile without a default constructor?

I can do this: #include int counter; int main() { struct Boo { Boo(int num) { ++counter; if (rand() % num < 7) Boo(8); } }; Boo(8); return 0; } This will compile…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
58
votes
3 answers

Is default no-args constructor mandatory for Gson?

Gson user guide states that we should define default no-args constructor for any class to work with Gson properly. Even more, in the javadoc on Gson's InstanceCreator class said that exception will be thrown if we try to deserialize instance of…
raindev
  • 1,017
  • 1
  • 12
  • 25
1
2 3
54 55