Advertisement

Operator Overloading

Started by May 03, 2001 10:59 AM
5 comments, last by Khrob 23 years, 6 months ago
Is it possible in c++ to overload the += and -= operators? thanks _________________________________ I used to be indecisive. Now I''m not so sure...
_________________________________I used to be indecisive. Now I'm not so sure...
It sure is, but they must be members of a class (or structure).

  class blah{    int val;    public:        int operator+=(int amount) {return val += amount;}        int operator-=(int amount) {return val += amount;}        int operator+=(float amount)            {return val = int(float(val) + amount + 0.5f);}        int operator++() {return ++val;} // prefix (++x); the int is only used to differentiate        int operator++(int) {return val++;} // postfix (x++)        int operator--() {return --val;} // prefix (--x)        int operator--(int) {return val--;} // postfix (x--)}  
Advertisement
You can overload nearly every operator in c++. These too...

cya
c_l
DOUBLE POST
cya
c_l

Edited by - c_l on May 3, 2001 12:19:08 PM
thanks for the speedy replies guys!

my vector class is suddenly cleaner!



_________________________________
I used to be indecisive. Now I''m not so sure...
_________________________________I used to be indecisive. Now I'm not so sure...
A handy operator to overload for a vector class is the float* operator like so:

operator float*(void) { return v; }
Where v is the internal vector.

This allows you to use your vectors in OpenGL calls like so:

glColor3fv(myVec);
glVertex3fv(myVec);

etc..., instead of something like this:

glColor3f(myVec[0], myVec[1], myVec[2]);
glVertex3f(myVec[0], myVec[1], myVec[2]);

Jason A.

---
I write code.
DelphiGL (http://delphigl.cfxweb.net)

Edited by - Jallen on May 3, 2001 2:00:19 PM
---I write code.DelphiGL (http://delphigl.cfxweb.net)
Advertisement
nice one mate.

cheers

_________________________________
I used to be indecisive. Now I''m not so sure...
_________________________________I used to be indecisive. Now I'm not so sure...

This topic is closed to new replies.

Advertisement