89

I'm implementing vector class and I need to get an opposite of some vector. Is it possible to define this method using operator overloading?

Here's what I mean:

Vector2f vector1 = -vector2;

Here's what I want this operator to accomplish:

Vector2f& oppositeVector(const Vector2f &_vector)
{
 x = -_vector.getX();
 y = -_vector.getY();
 
 return *this;
}
5
  • 7
    Style comment: don't prefix your variables with ''. This style is reserved for the implementation (compiler) and you may have conflicts. Also, readers my unintentionally recognize your variables as implementation variables because of the '' prefix. You don't need to use '_' inside functions; they will be readable without it. Commented Jan 28, 2010 at 18:48
  • 5
    Two underscores are reserved for implementation, not just one. Commented Jul 7, 2010 at 11:56
  • I believe that it's _MACROs and __identifiers. Commented Jul 7, 2010 at 12:20
  • 2
    Please tell me why you are using .getX() and .getY() on your simple Vector type Commented May 9, 2013 at 1:00
  • 4
    To be precise: Identifiers with two subsequent underscores anywhere are reserved – always, identifiers starting with an underscore followed by a captital letter are always reserved, too, any identifier starting with an underscore is reserved at global namespace. Commented Jun 28, 2019 at 8:05

2 Answers 2

147

Yes, but you don't provide it with a parameter:

class Vector {
   ...
   Vector operator-()  {
     // your code here
   }
};

Note that you should not return *this. The unary - operator needs to create a brand new Vector value, not change the thing it is applied to, so your code may want to look something like this:

class Vector {
   ...
   Vector operator-() const {
      Vector v;
      v.x = -x;
      v.y = -y;
      return v;
   }
};
Sign up to request clarification or add additional context in comments.

2 Comments

@Kornel Kisielewicz: Calling it "the only right solution" is misleading. Unary - can be overloaded by a standalone function as well, which would closely mirror the OP's implementation of oppositeVector.
Should return a const Vector.
36

It's

Vector2f operator-(const Vector2f& in) {
   return Vector2f(-in.x,-in.y);
}

Can be within the class, or outside. My sample is in namespace scope.

6 Comments

+1 for catching that you need to return a new Vector2f instance, not a reference to the current object.
This is the implementation of a binary operator-. Unary has no parameter as @anon said.
Nope, it is an unary operator. It takes a Vector2f as parameter since it is defined in namespace- rather than class scope. My response is correct.
See also 13.5.1 - 'A prefix unary operator shall be implemented by a non-static member function (9.3) with no parameters or a non-member function with one parameter'.
@shunz it's an unary operator. note it is namespace scope not class scope.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.