operator overloading question

I have two classes, Vector3D and Normal. I need to define two operators for the Normal class for the following operations:
1
2
3
4
5
Normal normal;
Vector3D vector;

double a = normal * vector;
double b = vector * normal;


The first one I have defined like so:
double operator *(const Vector3D& v) const;

But how do I define/implement the other one?
You can implement operator* as an external function ( declaring two parameters instead of just one )
uf u gonna declare it as external function, then u have to declre is as friend
uf u gonna declare it as external function, then u have to declre is as friend

That is not necessarily true; it depends on what the external function does. For example:

1
2
3
4
5
6
7
8
9
class MyComplexNumber
{
    public:
        MyComplexNumber operator+( int rhs ) const { /* ... */ }
};

// This need not be a friend since it just calls a public member of MyComplexNumber:
inline MyComplexNumber operator+( int lhs, const MyComplexNumber& rhs )
    { return rhs + lhs; }

Last edited on
Thanks, everyone, that's exactly what I needed!
Topic archived. No new replies allowed.