c++ - How to define an implicit conversion from double? -
i have defined class complex overloads + operator:
complex operator+(complex const& x, complex const& y); i want define implicit conversion double complex, such that, example, if write c + d, c complex , d double, call overloaded + defined above , return complex. how can this?
you need define constructor it. referred "converting constructor"
complex::complex(double x) { // conversion } this allow implicit conversion, long don't use explicit keyword, force have use cast convert.
you can define other versions of operator+
complex operator+(complex const& x, double y); complex operator+(double x, complex const& y);
Comments
Post a Comment