cast operators...

G

Gernot Frisch

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::eek:perator+(const A&)
or:
A operator+(double, const C&)

Any idea? I'm hopelessly confused how to manage what I want to do...


-Gernot
 
V

Victor Bazarov

Gernot said:
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::eek:perator+(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
 
J

John Harrison

Gernot Frisch said:
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::eek:perator+(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::eek:perator 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::eek:perator+(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
 
J

John Harrison

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
 
J

JKop

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::eek:perator+(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
 
G

Gernot Frisch

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::eek:perator+(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top