C++ classes, implicit casting and operator overloading -
i've run don't understand in regards class i've built , how interacts built-in types through operator overloading in c++. example of problem below (incomplete) complex number class:
class complex { public: complex(double real=0., double imag=0.) : _real(real), _imag(imag) {;} ~complex() {;} complex& operator=(const complex &rhs) { if(this!=&rhs) { this->_real = rhs._real; this->_imag = rhs._imag; } return *this; } complex& operator*=(const complex &rhs) { this->_real = this->_real * rhs._real + this->_imag * rhs._imag; this->_imag = this->_real * rhs._imag + this->_imag * rhs._real; return *this; } const complex operator*(const complex &other) const { complex result = *this; result *= other; return result; } protected: double _real; double _imag; };
when call code following main:
int main(void) { complex a(1.,0.); complex b; b = * 2.; b = 2. * a; return 0; }
i complier error second last line in "main.cpp":
error: no match ‘operator*’ in ‘2.0e+0 * a’
but no error line before. if cast "2.0" in offending line complex good. question is, how/why compiler know cast double complex number in first line (appears ) want use double version of operator* in second line?
if derive class, real, derives off of double , adds like:
const complex operator*(const double &other)
then think work know can't (built-in types can't used base-classes).
thanks!
@mikeseymore had nice fix. add non-member function. ended was:
complex operator*(double lhs, complex const &rhs) { complex result(lhs,0.); return result*=rhs; }
and world. mike.
btw: discussion of non-class overloading operator overloading outside class
since operator*
member function, conversions can applied right-hand operand. left-hand operand has of type complex
.
the simplest fix make non-member:
complex operator*(complex lhs, complex const & rhs) {return lhs *= rhs;}
if performance important, might provide specialisations double
, rather relying on implicit conversion, avoid unnecessary multiplications zero:
// 1 can member or friend, according taste complex operator*(double rhs) const {return complex(_real * rhs, _imag * rhs);} // 1 can't member complex operator*(double lhs, complex const & rhs) {return rhs * lhs;}
in general, shouldn't return const
values: inhibits move semantics.
Comments
Post a Comment