c++ overloading += operator -
i have class:
class myclass{ double a; double b; double c; double d; public: myclass(double _a, double _b, double _c, double _d){ = _a; b = _b; c = _c; d = _d; } myclass operator+=(const myclass & rhs){ += rhs.a; b += rhs.b; c += rhs.c; d += rhs.d; return this; } myclass operator+(myclass & rhs){ myclass newone(a+rhs.a,b+rhs.b,c+rhs.c,d+rhs.d); return newone; } }
and use this:
myclass my1(1., 2., 3., 4.); myclass my2(2., 3., 4., 5.); myclass my3(2., 4., 6., 8.); my2 += my3; my1 += my2;
and 1 working, when use this
my1 += my2 += my3;
i diffrent answer. , how can make count expression in brackets first? example :
(my1 + my2) + my3 == my1 + (my2 + my3)
it not clear result expecting in statement
my1 += my2 += my3;
the statement equivalent to
my1 += ( my2 += my3 );
according c++ standard (5.17 assignment , compound assignment operators)
1 assignment operator (=) , compound assignment operators all group right-to-left.
the valid operator definition like
myclass & operator +=( const myclass & rhs ) { += rhs.a; b += rhs.b; c += rhs.c; d += rhs.d; return *this; }
take account there several typos in code. example in operator instead of have return *this or class definition shall ended semicolon after closing brace.
as expression
(my1 + my2) + my3 == my1 + (my2 + my3)
then entirely unclear trying achieve. there no defined comparison operator class.
Comments
Post a Comment