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

# cast operators...

 P: n/a Hi, I have a problem. I want to be able to write this: a = b + d + c; Where a can be a double or class A; b,d,c can each indiviually be one of these: double, class A or class B. The result of an addition is always class A, or double (double + double can't be changed). Know what I mean? I need a real lot of operators and at one point I get ambiguity, since: A a; B b; double d; A = d; d = A; a = b + d + b; operator+ is ambigious: could be: A A::operator+(const A&) or: A operator+(double, const C&) Any idea? I'm hopelessly confused how to manage what I want to do... -Gernot Jul 22 '05 #1
5 Replies

 P: n/a Gernot Frisch wrote: I have a problem. I want to be able to write this: a = b + d + c; Where a can be a double or class A; b,d,c can each indiviually be one of these: double, class A or class B. The result of an addition is always class A, or double (double + double can't be changed). Know what I mean? I need a real lot of operators and at one point I get ambiguity, since: A a; B b; double d; A = d; d = A; a = b + d + b; operator+ is ambigious: could be: A A::operator+(const A&) or: A operator+(double, const C&) 'const C&'? Which one is 'C'? Did you mean 'B'? Are 'A' and 'B' related by any chance? Any idea? I'm hopelessly confused how to manage what I want to do... Post the minimal complete compilable program that demonstrates the situation. Post the error messages and mark the lines to which they relate. IOW, read the FAQ 5.8 and act on its recommendations. Victor Jul 22 '05 #2

 P: n/a "Gernot Frisch" wrote in message news:2n************@uni-berlin.de... Hi, I have a problem. I want to be able to write this: a = b + d + c; Where a can be a double or class A; b,d,c can each indiviually be one of these: double, class A or class B. The result of an addition is always class A, or double (double + double can't be changed). Know what I mean? I need a real lot of operators and at one point I get ambiguity, since: A a; B b; double d; A = d; d = A; a = b + d + b; operator+ is ambigious: could be: A A::operator+(const A&) or: A operator+(double, const C&) Any idea? I'm hopelessly confused how to manage what I want to do... Don't use cast operators. Its likely to cause ambiguity because you give the compiler two ways of making a conversion (do I construct A from B, or do I use B::operator A()). Overload operator+ with different types, but don't ever use the member form of operator+. I.e. prefer this A operator+(const A&, const A&); //global form to this A A::operator+(const A&) const // member form The reason is if you use the member form then the type conversion rules which apply to the left hand side of operator+ are different from the right hand side, which is confusing and rarely what you would want for operator+. If you get tired of writing all the possible combinations of operator+ consider using constructors for some of the conversions. E.g. Given class A { public: A(double); ... }; and A operator+(const A&, const A&); then A a; B b = a + 2.0; becomes legal because the A(double) constructor is called implicitly. john Jul 22 '05 #3

 P: n/a > Given class A { public: A(double); ... }; and A operator+(const A&, const A&); then A a; B b = a + 2.0; becomes legal because the A(double) constructor is called implicitly. I meant A b = a + 2.0; john Jul 22 '05 #4

 P: n/a Gernot Frisch posted: Hi, I have a problem. I want to be able to write this: a = b + d + c; Where a can be a double or class A; b,d,c can each indiviually be one of these: double, class A or class B. The result of an addition is always class A, or double (double + double can't be changed). Know what I mean? I need a real lot of operators and at one point I get ambiguity, since: A a; B b; double d; A = d; d = A; a = b + d + b; operator+ is ambigious: could be: A A::operator+(const A&) or: A operator+(double, const C&) Any idea? I'm hopelessly confused how to manage what I want to do... -Gernot Create only one operator to add A's together. Then supply conversion operators from double to A and so on. -JKop Jul 22 '05 #5

 P: n/a > Overload operator+ with different types, but don't ever use the member form of operator+. I.e. prefer this A operator+(const A&, const A&); //global form to this A A::operator+(const A&) const // member form The reason is if you use the member form then the type conversion rules which apply to the left hand side of operator+ are different from the right hand side, which is confusing and rarely what you would want for operator+. That was exaclty it! I removed the function from the class and whoppa - it worked! Thank you, Gernot Jul 22 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion. 