@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.