0

I have seen Boost give you the possibility of doing:

v*=c where c is i.e. a double and v is a vector

How could I make, as an exercise, the overloading of *= correctly, which works in the same way of the previous example (with double on the right side)?

2 Answers 2

2

How about something like this?

// in class
T& operator *= (double d)
{
   member *= d;
   return *this;
}

// outside of class
T operator * (T v, double d)
{
   v *= d;
   return v;
}
Sign up to request clarification or add additional context in comments.

5 Comments

You're missing an = in the second example
@maja it's not example, it's overloading of operator * just for consistence.
Then your code is modifying the parameter v, which shouldn't happen when you write v1 = v * 4.2;
@maja it's copy, real object will not be modified.
Damn, you're right. pass-by-value. Embarrassing that I didn't see it.
0

Your method will look as follows:

inline vector operator *= (vector v, double &b);

It's overloaded for the class vector, should take a double, should modify the object itself and should return itself to be used as v1 = v2*= 4.2;

Since it's for an exercise, you won't post the implementation here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.