next up previous contents
Next: Efficiencies: Up: The Overloaded Operator * Previous: The Overloaded Operator *

Some Basic Concerns:

We have to be careful with the definition of these operators. The first one

MYCOMPLEX& MYCOMPLEX::operator*=(const MYCOMPLEX &w)

is used in this sort of fragment

MYCOMPLEX z = 4.0 + 5.5*I;
z *= 3.0+3.5*I;

This first operator* = is called and it must replace the previous contents of z with this new version. So in the source code, we compute the complex multiplication in the fragment:

r = real*w.real - imaginary*w.imaginary;
imag = real*w.imaginary + imaginary*w.real;

note lines like

real = real*w.real - imaginary*w.imaginary;
imaginary = real*w.imaginary + imaginary*w.real;

will not work as the value of real is reset in the first line and so invalidates the computation in the second line.

Then, we reset the original z using the this pointer and return:

this->real = r;
this->imaginary = imag;
return *this;

The return of *this requires the return type of MYCOMPLEX& or MYCOMPLEX. We must choose MYCOMPLEX& so that we reset the original value of z.

MYCOMPLEX& MYCOMPLEX::operator*=(const MYCOMPLEX &w)
{
double r,imag;
r = real*w.real - imaginary*w.imaginary;
imag = real*w.imaginary + imaginary*w.real;
this->real = r;
this->imaginary = imag;
return *this;
}

This second overloaded operator* =

MYCOMPLEX operator*=(const int& i,const MYCOMPLEX& w)
{
MYCOMPLEX T;
T.real *= (double)i;
T.imaginary *= (double)i;
return T;
}

is a friend function and might be used in lines like

MYCOMPLEX z = 2.3 + 5.6*I;
int k = 1;
k *= z;

But does this sort of operation make sense? Does the implementation make sense? First, note that since w comes in as a constant, we are permitted to alter its attributes. So that is the reason we are using a temporary variable T to store the new values. Upone return from this function call, this local copy must be used to update the integer variable k; ie the result from the function must be stored in the integer k. This is not possible unless the type of k is changed to be MYCOMPLEX. This is an example of a nonsensical operator. Similar comments can be made about operators in this order using the float and double variables.

So we will remove these operators from our MYCOMPLEX class.

However, the operator which uses the operation in the reverse order is just fine to use as the result is used to update the value of the left hand side complex number. The source code for this function is:

MYCOMPLEX operator*=(MYCOMPLEX& w,const int& i)
{
w.real = (double)i*w.real;
w.imaginary = (double)i*w.imaginary;
return w;
}

and returns a local copy of w. This is used in situations like this:

MYCOMPLEX z = 2.3 + 5.6*I;
z *= 1;

In either case, note the source code. We reset the incoming w complex number and then return it as a value. Note if we send in

const MYCOMPLEX& w

we will not be allowed to change w because we can't alter the contents of a constant variable. Hence the following prototype and implementation for this kind of friend overloaded operator * = is not correct:

MYCOMPLEX operator*=(const int& i,const MYCOMPLEX& w)
{
w.real = (double)i*w.real;
w.imaginary = (double)i*w.imaginary;
return w;
}

There are further inefficiencies here which we will discuss in a moment. For now, note that the rest of the friend overloaded operator * = are handled in a similar fashion and are just listed here for completeness.

 
MYCOMPLEX operator*=(MYCOMPLEX& w,const float& x)
{
w.real = (double)x*w.real;
w.imaginary = (double)x*w.imaginary;
return w;
}
MYCOMPLEX operator*=(MYCOMPLEX& w,const double& x)
{
w.real = x*w.real;
w.imaginary = x*w.imaginary;
return w;
}

So to finish up this part, let's be clear: we should not implement the operators:

    friend MYCOMPLEX operator*=(int& i,const MYCOMPLEX& w);
    friend MYCOMPLEX operator*=(float& x,MYCOMPLEX& w);
    friend MYCOMPLEX operator*=(double& x,MYCOMPLEX& w);

as it is nonsense to try to update the values of int, float or double variables with a complex number.


next up previous contents
Next: Efficiencies: Up: The Overloaded Operator * Previous: The Overloaded Operator *
Jim Peterson
1999-04-22