473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

complex number operator question

Suppose you have a complex number class, and you overload conversions
to double by only taking the real part. You also overload operator* to
do complex multiplication.

You then write:

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

complex c = a*b;

Then the compiler complains that it is ambiguous because it doesn't
know if you want complex*complex, or to do the case to double first,
and do double*double. I would think that it would be obvious that
complex*complex should be chosen. Other than explicit casts is there a
way to handle this?

Dec 15 '06 #1
2 1880
jraul wrote:
Suppose you have a complex number class, and you overload conversions
to double by only taking the real part. You also overload operator*
to do complex multiplication.

You then write:

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

complex c = a*b;

Then the compiler complains that it is ambiguous because it doesn't
know if you want complex*complex, or to do the case to double first,
and do double*double. I would think that it would be obvious that
complex*complex should be chosen. Other than explicit casts is there
a way to handle this?
Make sure that the operator* is actually a better choice for the
compiler, i.e. it doesn't have to perform any conversions before
calling it. I think it is possible. Read the FAQ 5.8, I think it
deals with your case.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 15 '06 #2
jraul wrote:
Suppose you have a complex number class, and you overload conversions
to double by only taking the real part. You also overload operator* to
do complex multiplication.

You then write:

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

complex c = a*b;

Then the compiler complains that it is ambiguous because it doesn't
know if you want complex*complex, or to do the case to double first,
and do double*double. I would think that it would be obvious that
complex*complex should be chosen. Other than explicit casts is there a
way to handle this?
After all I don't think the code above is good.
Though you may find a way to make non-conversion calculation the
"preferred" way for one compiler, you can't ensure that other compilers
accept it. Explicit cast is extremely recommend to prevent ambiguous
overloaded-function call. Scott Meyers mentioned this in his Effective
C++.
So write the cast-constructor as explicit, it is good for your program.

Sincerely,
Z.Z.

Dec 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
3
by: Arthur | last post by:
Spending the morning avoiding responsibilities, and seeing what it would take to color some complex numbers. class color_complex(complex): def __init__(self,*args,**kws):...
8
by: Steve Jorgensen | last post by:
Mailing List management is a good example of a case where my conundrum arises. Say there is a m-m relationship between parties and groups - anyone can be a member of any combintation of groups. ...
2
by: jccorreu | last post by:
I'm taking in data from multiple files that represents complex numbers for charateristics of different elements. I begin with arrays of doubles and interpolate to get standard values in real and...
2
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator...
7
by: john134 | last post by:
Complex Complex::operator+( const Complex &operand2 ) const { return Complex( real + operand2.real, imaginary + operand2.imaginary ); } this is my addition operator for 2 complex numbers. e.g....
1
by: jraul | last post by:
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(...);...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
5
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.