What is conversion constructor in C++?

01 Sep, 2021
Pre-requisite: Type Conversion in C++ and Use of explicit keyword in C++ 
In C++, if a class has a constructor which can be called with a single argument, then this constructor becomes conversion constructor because such a constructor allows automatic conversion to the class being constructed. 
Example  
C++
#include <iostream> class MyClass {    int a, b; public:    MyClass(int i)    {        a = i;        b = i;    }    void display()    {        std::cout << " a = " << a << " b = " << b << "\n";    }}; int main(){    MyClass object(10);    object.display();     // Single parameter conversion constructor is invoked.    object = 20;    object.display();    return 0;}
Output: 
a = 10 b = 10
a = 20 b = 20
Conversion Constructors: There are constructors that convert types of its parameter into a type of the class. The compiler uses these constructors to perform implicit class-type conversions. These conversions are made by invoking the corresponding constructor with matches the list of values/objects that are assigned to the object.
This previous example only deals with one parameter, to extend it to several parameters i.e., extended initializer lists or braced-init-lists. That is, we enclose the parameters to be passed to it inside a pair of curly braces ({}).
Example with Multiple Parameters: 
C++
#include <iostream> class MyClass {    int a, b; public:    MyClass(int i, int y)    {        a = i;        b = y;    }    void display()    {        std::cout << " a = " << a << " b = " << b << "\n";    }}; int main(){    MyClass object(10, 20);    object.display();     // Multiple parameterized conversion constructor is invoked.    object = { 30, 40 };    object.display();    return 0;}
Output: 
a = 10 b = 20
a = 30 b = 40
Note:  
Extended initializer lists are available in C++11 and on.We can directly try assigning a extended initializer list to a object when it is being created.An implicit class-type conversion feature of a constructor doesn’t affect its normal behavior.Using the explicit function-specifier for a constructor removes the implicit conversions using that constructor
Extended initializer lists are available in C++11 and on.
We can directly try assigning a extended initializer list to a object when it is being created.
An implicit class-type conversion feature of a constructor doesn’t affect its normal behavior.
Using the explicit function-specifier for a constructor removes the implicit conversions using that constructor
Usage of conversion constructor  
1. In return value of a function: 
When the return type of a function is a class, instead of returning a object, we can return a braced-init-list, now since the return type is a class instance, a object of that class is created with the braced-init-list, given that the class has a corresponding conversion constructor.
Example:  
MyClass create_object(int x, int y) { return {x, y}; }
This function create_object will return a MyClass object with a and b values as the x and y passed to the function. 
2. As a parameter to a function: When a function’s parameter type is of a class, instead of passing a object to the function, we can pass a braced-init-list to the function as the actual parameter, given that the class has a corresponding conversion constructor. 
An example :  
void display_object(MyClass obj) { obj.display(); }// This function is invoked in the main function with a braced-init-list with two integers as the parameter. // e.g. : // display_object({10, 20});  
Note: This function display_object creates a new class instance of MyClass called obj and will call its member function display().
Credits: Bharath Vignesh J K
BharathVigneshJK
surindertarika1234
adnanirshad158
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.