446,260 Members | 1,338 Online
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

 P: n/a Hi, when Do I need the: a a::operator+(a _1); and when the a operator+(a _1, a _2) ?? -- -Gernot int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);} ________________________________________ Looking for a good game? Do it yourself! GLBasic - you can do www.GLBasic.com Jul 22 '05 #1
5 Replies

 P: n/a Gernot Frisch wrote: Hi, when Do I need the: a a::operator+(a _1); This is a member function and when the a operator+(a _1, a _2) This is a non-member function. ?? I think this example might help clear things up a little. struct A { A operator + ( const A & ); }; struct B { }; B operator + ( const B &, const B & ); B operator + ( const B &, const A & ); B operator + ( const A &, const B & ); void foo() { B b1; B b2; A a1; A a2; b1 = b1 + b2; b1 = a1 + b1; b1 = b1 + a1; a1 = a1 + a2; } Jul 22 '05 #2

 P: n/a > I think this example might help clear things up a little. Not completly struct B; struct A { A operator + ( const A & ); // What about: A operator +(const B&); }; struct B { }; versus this one?? B operator + ( const A &, const B & ); Thank you, Gernot Jul 22 '05 #3

 P: n/a Gernot Frisch wrote: Hi, when Do I need the: a a::operator+(a _1); and when the a operator+(a _1, a _2) ?? The first is a member function, while the second is not. The difference is as follows: Assume class A { public: A( int i ) : m_i( i ) {} private: int m_i; } That is: there is a class where objects can implicitely be constructed from an int. Now assume you want to write an op+ to add 2 A objects. You do this as member function class A { .... A operator+( const A& Arg ) { .... } }; Now what can you do with it? You can eg. write A ObjA( 5 ); A ObjB( 7 ); A Result; Result = ObjA + ObjB; No problem. But you can also write Result = ObjA + 7; Why? Because the compiler can use the constructor to first convert 7 into an A object and then use op+ to perform the addition. But can you also write Result = 7 + ObjA; And the answer is: No. Because 7 is an int, and there is no op+ for an int which takes an A object. This seems illogical and it can be cured by making op+ a non member function A operator+( const A& lhs, const A& rhs ) { ... } Now the compiler can use this operator as long as there is one A object on either side of the '+'. The other one will be converted to an A object if necessary. -- Karl Heinz Buchegger kb******@gascad.at Jul 22 '05 #4

 P: n/a > This seems illogical and it can be cured by making op+ a non member function A operator+( const A& lhs, const A& rhs ) { ... } Now the compiler can use this operator as long as there is one A object on either side of the '+'. The other one will be converted to an A object if necessary. Now, I toally got this! Thank you a lot for you explaination. -Gernot Jul 22 '05 #5

 P: n/a Gernot Frisch asked: when Do I need the: a a::operator+(a _1); and when the a operator+(a _1, a _2) Karl Heinz Buchegger replied: The first is a member function, while the second is not. .... A operator+( const A& lhs, const A& rhs ) { ... } It has been suggested to implement operator+ in terms of member operator+= (e.g., last Friday by Richard Herring, on this newsgroup). Of course, you should have operator+= as follows: A& A::operator+=(const A&) { // TODO: The addition itself! return *this; } If you do, is there a difference between the following two versions? A operator+(const A& _1, const A& _2) { return A(_1) += _2; } and: A operator+(A _1, const A& _2) { return _1 += _2; } Which one is preferable? Will it make any difference to the caller of operator+? Niels Dekker www.xs4all.nl/~nd/dekkerware Jul 22 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.