Open In App

What is conversion constructor in C++?

Last Updated : 15 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: Type Conversion in C++ and Use of explicit keyword in C++ 

A conversion constructor is a single-parameter constructor that is declared without the function specifier explicitly. The compiler uses conversion constructors to convert objects from the type of the first parameter to the type of the conversion constructor’s class.

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:  

  1. Extended initializer lists are available in C++11 and on.
  2. We can directly try assigning an extended initializer list to an object when it is being created.
  3. An implicit class-type conversion feature of a constructor doesn’t affect its normal behavior.
  4. 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 an object, we can return a braced-init-list, now since the return type is a class instance, an 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 an 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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads