complex number overload

Discussion in 'C++' started by jraul, Dec 15, 2006.

  1. jraul

    jraul Guest

    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?
     
    jraul, Dec 15, 2006
    #1
    1. Advertising

  2. jraul

    rossum Guest

    On 15 Dec 2006 09:47:08 -0800, "jraul" <> wrote:

    >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
     
    rossum, Dec 15, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    602
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    392
    Nick Hounsome
    Jan 25, 2004
  3. Replies:
    2
    Views:
    474
  4. jwest
    Replies:
    7
    Views:
    455
    Gianni Mariani
    Apr 6, 2008
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    277
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page