Operator Overloading
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--)}
thanks for the speedy replies guys!
my vector class is suddenly cleaner!
_________________________________
I used to be indecisive. Now I''m not so sure...
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
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)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement