Questions tagged [constructor-overloading]

Constructor overloading is used to increase the flexibility of a class by having alternative constructors for a single class. Having more than one way of initializing objects can be achieved using overloading constructors.

Constructors, used to create instances of an object, may also be overloaded in some object oriented programming languages.

Because in many languages the constructor’s name is predetermined by the name of the class, it would seem that there can be only one constructor.

Whenever multiple constructors are needed, they are implemented as overloaded functions.

A default constructor takes no parameters, instantiating the object members with a value zero.

Caveats

If a method is designed with an excessive number of overloads, it may be difficult for developers to discern which overload is being called simply by reading the code.

This is particularly true if some of the overloaded parameters are of types that are inherited types of other possible parameters (for example "object").

An IDE performs the overload resolution and displays (or navigates to) the correct overload.

References

Related tags

216 questions
117
votes
5 answers

Constructor overloading in Java - best practice

There are a few topics similar to this, but I couldn't find one with a sufficient answer. I would like to know what is the best practice for constructor overloading in Java. I already have my own thoughts on the subject, but I'd like to hear more…
Eyal Roth
  • 3,895
  • 6
  • 34
  • 45
74
votes
3 answers

C# constructors overloading

How I can use constructors in C# like this: public Point2D(double x, double y) { // ... Contracts ... X = x; Y = y; } public Point2D(Point2D point) { if (point == null) ArgumentNullException("point"); …
Aleksandr Vishnyakov
  • 1,872
  • 5
  • 23
  • 39
60
votes
3 answers

Transfer NULL to the constructor

I can not understand why the constructor is executed with the parameter Double[]? using System.Collections.Generic; using System.Linq; using System.Text; namespace MyConsoleApp { class Program { static void Main(string[] args) …
Amazing User
  • 3,473
  • 10
  • 36
  • 75
48
votes
8 answers

What does "this()" method mean?

I ran into this block of code, and there is this one line I don't quit understand the meaning or what it is doing. public Digraph(In in) { this(in.readInt()); int E = in.readInt(); for (int i = 0; i < E; i++) { int v =…
Sugihara
  • 1,091
  • 2
  • 20
  • 35
40
votes
3 answers

Why is template constructor preferred to copy constructor?

#include struct uct { uct() { std::cerr << "default" << std::endl; } uct(const uct &) { std::cerr << "copy" << std::endl; } uct( uct&&) { std::cerr << "move" << std::endl; } uct(const int &) { std::cerr << "int"…
31
votes
2 answers

How to overload constructors in JavaScript ECMA6?

Objective Implement a mechanism to allow constructor overload in JavaScript ECMA6 Why this is not a duplicate The topic Why doesn't JavaScript ES6 support multi-constructor classes?, although similar is not the same as this one. The other topic…
29
votes
1 answer

Typescript class: "Overload signature is not compatible with function implementation"

I created the following class: export class MyItem { public name: string; public surname: string; public category: string; public address: string; constructor(); constructor(name:string, surname: string, category: string, address?:…
smartmouse
  • 13,912
  • 34
  • 100
  • 166
22
votes
2 answers

Something interesting about two overloaded constructors of FileInputStream in Java 7 API

Before talking about FileInputStream, I am starting with a scenario where there are two perfectly valid, overloaded methods but where the compiler will get confused and then report a compile-time error in response to certain inputs. Here are the…
19
votes
1 answer

Why doesn't std::string have a constructor that directly takes std::string_view?

To allow std::string construction from std::string_viewthere is a template constructor template explicit basic_string(const T& t, const Allocator& alloc = Allocator()); which is enabled only if const T& is convertible to…
oliora
  • 839
  • 5
  • 12
18
votes
2 answers

C++ how to generate all the permutations of function overloads?

Lets say I have classes Date and classes Year, Month and Day. struct Date { Date(Year year, Month month, Day day) : d(day), m(month), y(year) {}; Date(Month month, Day day, Year year) : d(day), m(month), y(year) {}; Date(Day day, Month month,…
lukas.pukenis
  • 13,057
  • 12
  • 47
  • 81
16
votes
1 answer

Is there a convenience constructor in C++?

Is it possible for an overloaded constructor to somehow call another constructor within the class, similar to the code below? class A { public: A(std::string str) : m_str(str) {} A(int i) { *this = std::move(A(std::to_string(i))); } …
Makaronodentro
  • 907
  • 1
  • 7
  • 21
14
votes
3 answers

Why const char* implicitly converted to bool rather than std::string?

#include #include struct mystruct{ mystruct(std::string s){ std::cout<<__FUNCTION__ <<" String "<
11
votes
3 answers

class constructor precedence with a variadic template constructor for a value wrapper

Today I've discovered that I don't understand the C++ constructor precedence rules. Please, see the following template struct wrapper template struct wrapper { T value; wrapper (T const & v0) : value{v0} { std::cout <<…
max66
  • 65,235
  • 10
  • 71
  • 111
10
votes
6 answers

Why is my overloaded C++ constructor not called?

I have a class like this one: class Test{ public: Test(string value); Test(bool value); }; If I create an object like this: Test test("Just a test..."); The bool constructor is called! Anyone knows why? Thanks
user413185
  • 101
  • 3
8
votes
2 answers

Strange behavior of std::initializer_list of std::strings

This question is already asked most likely, but I did not find the answer. The code below compiles with gcc but crashes at runtime, with std::length_error (live). void test(const std::string &value) { std::cout << "string overload: " << value <<…
1
2 3
14 15