c++ - What are the conversion constructors -


class complex { private: double real; double imag;  public: // default constructor complex(double r = 0.0, double = 0.0) : real(r), imag(i) {}  // method compare complex numbers bool operator == (complex rhs) {    return (real == rhs.real && imag == rhs.imag)? true : false; } };  int main() { // complex object complex com1(3.0, 0.0);  if (com1 == 3.0)    cout << "same"; else    cout << "not same";  return 0; } 

output: same

why code gives output same, how conversion constructor in working here, please explain, many many in advance

a conversion constructor non-explicit constructor callable 1 argument. in code sample, complex constructor provides default values parameters, can called single argument (say 3.0). , since such constructor not marked explicit, valid conversion constructor.

when com1 == 3.0 --given there no operator == between complex , double-- conversion constructor invoked. code equivalent this:

if( com1 == complex(3.0) ) 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -