Look at the C++14 tab in
http://www.cplusplus.com/reference/vector/vector/vector/
The std::vector seems to have
10 constructors. Is that more complex than with two constructors? No.
C++ has
function overloading. There can be more than one function with identical name, but different parameters. When the name is used (a function is called), the compiler searches from the list of declared names the one that matches best to the parameter types. Implicit conversions of the parameters are allowed (unless constructor declaration requires
explicit
types).
Lets add to the fiji885's example:
1 2 3 4
|
int main() {
double bar { 3.14 };
myclass foo( bar );
}
|
The declaration of foo seems to require
myclass::myclass(double)
However, the available declarations are:
1 2 3
|
myclass::myclass() #1
myclass::myclass(const int) #2
myclass::myclass(const myclass&) #3
|
The #3 is copy constructor, which the compiler automatically generates (except in special cases).
Can the compiler match any of these to the actual call?
There is no direct match, but compiler can add some implicit conversions.
Is there an unambiguous winner then?
Yes, casting double into int makes #2 a valid choice.
There is no cast from double to nothing (except in game shows).
Casting from double to myclass is the whole dilemma, so #3 is out.
Therefore, the compiler inserts call to
myclass::myclass(const int)
.
PS. C++11 did add
delegating constructor syntax. With that one constructor can call an another constructor of the same class.