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

Started by
8 comments, last by executor_2k2 21 years, 9 months ago
What''s the syntax for overloading the unary "-" operator? Thanks.
Well, that was a waste of 2 minutes of my life. Now I have to code faster to get 'em back...
Advertisement
It has to be a member function, returning an object by value.

Foo Foo::operator-() { Foo tmp; tmp.member = -member; return tmp; }

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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;}  
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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Nevermind, Fruny fixed his post.



[edited by - Null and Void on June 28, 2002 4:09:10 AM]
I''m so used to seeing that error that I didn''t notice immediately that it was another error.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
Well, that was a waste of 2 minutes of my life. Now I have to code faster to get 'em back...
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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]
Well, that was a waste of 2 minutes of my life. Now I have to code faster to get 'em back...
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]
SysOp_1101

This topic is closed to new replies.

Advertisement