Let’s say we have a class called Complex which represents a complex number.
I want to convert this object to a double object.
The other way around i can do by implementing a copy ctor in Complex:
Complex(const double &d);
However, i can’t implement i copy ctor in double which will receive a Complex.
How do i do this? I know there is a way with operator overloading, but i couldn’t find how.
Eventually i want the this line will compile:
Complex c;
(double)c;
Thanks!!
Implement a conversion operator on your Complex class:
If for whatever reason you don’t want to muck about with this, you can provide a global conversion function: