Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

executor_2k2

What's the syntax for overloading the unary "-" operator?

This topic is 5929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Guest Anonymous Poster
Here''s an example:


  
#include <iostream>

using namespace std;

class A
{
private:
int i;
public:
A(int i_) : i(i_) {}
~A() {}
int& operator-() { i = -i; return i; }
};

int main()
{
A a(5);
cout << -a << endl;
return 0;
}

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
int& operator-() { i = -i; return i; }



You're modifing the object itself.
You've just lost your C++ programmer license.
Go direcly to Jail. Do not pass Go. Do not get $200.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 28, 2002 4:07:41 AM]

Share this post


Link to post
Share on other sites
I tried that but im getting an error that says:

error C2679: binary ''='' : no operator found which takes a right-hand operand of type ''int'' (or there is no acceptable conversion)

My overload looks like this:
inline &CVector operator-()
{ return CVector(-x, -y, -z); }

and my overloaded equals looks like this:

inline CVector operator=(const CVector &vec) const
{ return CVector(vec.x, vec.y, vec.z);};

Anyone know whats up? Thanks.

Share this post


Link to post
Share on other sites

class CVector
{
double x,y,z;
public:
explicit CVector( double x = 0, double y = 0, double z = 0 )
: x( x ), y( y ), z( z )
{}

CVector operator-() { return CVector(-x,-y,-z); }
};


1) For a 3d-vector, you don''t need and overloaded operator=
2) operator- must return by value. Otherwise BAD things happen.
3) BTW: an overloaded operator= must return *this, by reference.

4) your error is probably that you''ve written ''CVector v = 1'';

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
my error is this: pObject->pLocalCoord[2] = -tempYAxis;

LocalCoord is an array of CVector and tempYAxis is a CVector.

Also, why doesnt a 3d vector need to have an overloaded = ? Is that the same with matrices? If not, should my overloaded = for matrices return *this too? Why *this. I can see why, but i just wanna make sure. THanks.

[edited by - executor_2k2 on June 28, 2002 4:33:42 AM]

Share this post


Link to post
Share on other sites
quote:

Also, why doesnt a 3d vector need to have an overloaded = ?



Because C++ has no problem with assignment of it's built in data types in the default '=' member function. If you were using pointers to primitive data types however the situation would be different.

quote:

Is that the same with matrices?



Yes.

quote:

If not, should my overloaded = for matrices return *this too?



As I recall, according to the C++ standard, an overloaded '=' function must always return a refrence to it's own class type.

quote:

Why *this. I can see why, but i just wanna make sure. THanks.



It allows you to do multiple assignments like so...foo1 = foo2 = foo3. You could try to do something nutty like return a pointer to a different object of the same type, but it's not exactly good coding style. Besides that, I can't think of anything to be gained by doing it especially since it would butcher the data. In the scenerio I just presented, foo1 and foo2 would have the same value as each other but a completely different value than the source value of foo3, which is not the desired effect (at least if you want your code to actually be useable).

There are other reasons which I'm sure others will tell you. That was just the most practicle reason/example that came to mind.

[edited by - SysOp_1101 on June 28, 2002 5:26:54 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!