Overloading an operator*

I am learning C++ basics in a class at school, and the professor provided the following example of overloaded operators:

#include <iostream>
using namespace std;

class A
{
public:
A operator*(int  I){A a;cout<<" operator a*2; "<<endl;return a; }
};
A operator*(int  I,A b){A a;cout<<" operator 2*a;"<<endl;return a; } //9
int main()
{
A a;
a*2;
2*a;
cout<<endl;
return 0;
}
////OUTPUT WHEN PROGRAM RUNS
operator a*2; //19
operator 2*a; //20

Note in line 9 (indicated //9) there are two parameters in the declaration of the operator*, an int and an A object. The program compiled and ran as shown in lines 19 and 20.

My current project is also for school, but I'm not asking this forum to help me with anything major on the project. I am simply stuck on a single declaration for an operator* that is similarly overloaded as the example above. The class is called Complex and the declaration is as follows:

Complex operator* (double I, const Complex& j) const;

The declaration of the implementation of the operator* function (outside of the class declaration) is as follows:

Complex Complex::operator* (double i, const Complex j) const

However, I'm getting the following error when trying to compile:

'Complex Complex::operator* (double i, const Complex j) const' must take either zero or one argument

My question is, why did this type of declaration compile on the previous class but not this one? The previous example took two arguments. In this project, creating Complex objects, I would not be able to write statements like line 4 below, similar to the statement in line 20 of the first example:

Complex a;
Complex b;
Complex c;
c = 2.0 * b; //4

Any input on this would be greatly appreciated.
the professor provided the following example


I don't want to speak out of turn, but that is an absolutely HORRIBLE example. I don't think your professor is as versed in C++ as he should be. He is doing several things very, very wrong... the biggest of which is using uninitialized variables.

But anyway... to answer your questions...

My question is, why did this type of declaration compile on the previous class but not this one?


The binary * operator (the one you're overloading) always takes 2 arguments: one for each side of the * operator.

ie:
 
foo * bar // foo is the first argument, bar is the 2nd 


The tricky part is that the compiler will automatically use the hidden 'this' parameter as the first argument if you declare the operator inside the class. In which case, you only need one additional parameter.

To clarify:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Foo
{
  // inside the class... only use 1 parameter
  Foo operator* (const Foo& r) const
  {
    // the 'this' object is the first parameter (left side of * operator)
    // 'r' is the second parameter (right side of * operator)
  }
};

// outside of class:
Foo operator* (const Foo& l, const Foo& r)
{
  // since this is not a class member, there is no 'this' object.
  //  therefore...
  // 'l' is the first parameteter (left side of * operator)
  // 'r' is the second paramter (right side of * operator)
}
Last edited on

@Disch
I don't want to speak out of turn, but that is an absolutely HORRIBLE example. I don't think your professor is as versed in C++ as he should be. He is doing several things very, very wrong... the biggest of which is using uninitialized variables.



Why do you think that the example is "absolutely HORRIBLE". And where did you see "uninitialized variables"?

There are two overload operator *. One is defined in the class that is it is a class member

A operator*(int I){A a;cout<<" operator a*2; "<<endl;return a; }
};

and can be used for expressions like A * int.

And the other is defined outside the class and is not a member of the class.

A operator*(int I,A b){A a;cout<<" operator 2*a;"<<endl;return a; } //9

It can be used for expressions like int * A.

The only "HORRIBLE" things that I see is that the operator that is a member of the class shall be declared with qualifier const. And the operator that is not a member of the class shall declare the second parameter as a const reference to A.

But the absence of the const qualifier can be explained that they did not learn yet all cases of using the const qualifier and did not learn what the reference is.

So I do not see nothing "HORRIBLE" in the example.:)

Of course you could write something as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A
{
public:
   A operator *( int ) const 
   { 
      A a; 
      cout<< " operator a*2; " << endl; 
      return a; 
   }
};

A operator *( int, const A & )
{ 
   A a; 
   cout << " operator 2*a;" << endl;
   return a; 
} //9 


But you should take into account the learning path.
Last edited on
Yeah actually, you're totally right. I way overreacted.

I guess I was expecting a functional example and that example isn't functional, it only shows the syntax for declaring the operator.


I need to chillax.
Topic archived. No new replies allowed.