Questions tagged [constructor]

A special type of subroutine called at the creation of an object.

In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created.

20916 questions
3615
votes
11 answers

What does the explicit keyword mean?

What does the explicit keyword mean in C++?
Skizz
  • 69,698
  • 10
  • 71
  • 108
2648
votes
22 answers

How do I call one constructor from another in Java?

Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if there are several ways to do it)?
ashokgelal
  • 80,002
  • 26
  • 71
  • 84
2309
votes
23 answers

What is the best way to give a C# auto-property an initial value?

How do you give a C# auto-property an initial value? I either use the constructor, or revert to the old syntax. Using the Constructor: class Person { public Person() { Name = "Initial Name"; } public string Name { get;…
bentford
  • 33,038
  • 7
  • 61
  • 57
1829
votes
10 answers

Calling the base constructor in C#

If I inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how do I do that? For example, if I inherit from the Exception class I want to do something like this: class…
lomaxx
  • 113,627
  • 57
  • 144
  • 179
1470
votes
18 answers

Virtual member call in a constructor

I'm getting a warning from ReSharper about a call to a virtual member from my objects constructor. Why would this be something not to do?
JasonS
  • 23,480
  • 9
  • 41
  • 46
1301
votes
14 answers

Call one constructor from another

I have two constructors which feed values to readonly fields. public class Sample { public Sample(string theIntAsString) { int i = int.Parse(theIntAsString); _intField = i; } public Sample(int theInt) => _intField =…
Avi
  • 15,696
  • 9
  • 39
  • 54
1132
votes
15 answers

Can I call a constructor from another constructor (do constructor chaining) in C++?

As a C# developer I'm used to running through constructors: class Test { public Test() { DoSomething(); } public Test(int count) : this() { DoSomethingWithCount(count); } public Test(int count, string name) :…
Stormenet
  • 25,926
  • 9
  • 53
  • 65
1108
votes
8 answers

Do the parentheses after the type name make a difference with new?

If 'Test' is an ordinary class, is there any difference between: Test* test = new Test; and Test* test = new Test();
David Read
  • 11,031
  • 3
  • 17
  • 5
940
votes
15 answers

What is a clean "pythonic" way to implement multiple constructors?

I can't find a definitive answer for this. As far as I know, you can't have multiple __init__ functions in a Python class. So how do I solve this problem? Suppose I have a class called Cheese with the number_of_holes property. How can I have two…
winsmith
  • 20,791
  • 9
  • 39
  • 49
905
votes
24 answers

How to initialize HashSet values by construction?

I need to create a Set with initial values. Set h = new HashSet(); h.add("a"); h.add("b"); Is there a way to do this in one line of code? For instance, it's useful for a final static field.
Serg
  • 13,470
  • 8
  • 36
  • 47
846
votes
10 answers

What are the rules for calling the base class constructor?

What are the C++ rules for calling the base class constructor from a derived class? For example, I know in Java, you must do it as the first line of the subclass constructor (and if you don't, an implicit call to a no-arg super constructor is…
levik
  • 114,835
  • 27
  • 73
  • 90
730
votes
22 answers

Can an abstract class have a constructor?

Can an abstract class have a constructor? If so, how can it be used and for what purposes?
Szere Dyeri
  • 14,916
  • 11
  • 39
  • 42
718
votes
22 answers

Why do this() and super() have to be the first statement in a constructor?

Java requires that if you call this() or super() in a constructor, it must be the first statement. Why? For example: public class MyClass { public MyClass(int x) {} } public class MySubClass extends MyClass { public MySubClass(int a, int b)…
Joe Daley
  • 45,356
  • 15
  • 65
  • 64
708
votes
8 answers

What is the difference between using constructor vs getInitialState in React / React Native?

I've seen both used interchangeably. What are the main use cases for both? Are there advantages / disadvantages? Is one a better practice?
Nader Dabit
  • 52,483
  • 13
  • 107
  • 91
629
votes
16 answers

Interface defining a constructor signature?

It's weird that this is the first time I've bumped into this problem, but: How do you define a constructor in a C# interface? Edit Some people wanted an example (it's a free time project, so yes, it's a game) IDrawable +Update +Draw To be able to…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
1
2 3
99 100