What is conversion constructor 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.
#include<iostream> using namespace std; class Test { private: int x; public: Test(int i) {x = i;} void show() { cout<<" x = "<<x<<endl; } }; int main() { Test t(20); t.show(); t = 30; // conversion constructor is called here. t.show(); getchar(); return 0; } |
chevron_right
filter_none
The above program prints:
x = 20
x = 30
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Copy constructor vs assignment operator in C++
- When is copy constructor called?
- When should we write our own copy constructor?
- Does C++ compiler create default constructor when we write our own?
- C++ default constructor | Built-in types
- Advanced C++ | Conversion Operators
- Advanced C++ | Virtual Constructor
- Advanced C++ | Virtual Copy Constructor
- Catch block and type conversion in C++
- Why copy constructor argument should be const in C++?
- Is it possible to call constructor and destructor explicitly?
- Conversion of Struct data type to Hex String and vice versa
- __attribute__((constructor)) and __attribute__((destructor)) syntaxes in C
- Copy Constructor in C++
- Order of Constructor/ Destructor Call in C++



