complex number overload

J

jraul

Suppose we have a complex number class and we overload the conversion
to double to return the real part. We also overload operator* to do
complex multiplication. Consider now:

complex a(...);
complex b(...);

complex c = a*b;

But the compiler complains ambiguity since it doesn't know whether to
do complex*complex, or convert to double first and do double*double.
Other than explicit casting is there a nice solution?
 
R

rossum

Suppose we have a complex number class and we overload the conversion
to double to return the real part. We also overload operator* to do
complex multiplication. Consider now:

complex a(...);
complex b(...);

complex c = a*b;

But the compiler complains ambiguity since it doesn't know whether to
do complex*complex, or convert to double first and do double*double.
Other than explicit casting is there a nice solution?
Don't overload the conversion to double, use a.real() and a.imag()
instead. With no conversion to double the compiler only has one
option: complex * complex.

rossum
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top