Mason McCuskey
Spin Studios
www.spin-studios.com
Overloading operator[]; Need Some Light
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
Thanx for your replies, but they're a little vague. Could you please be more specific, maybe some sample code would be nice. I know a lot of theory of OOP, but I have very little practice, and it helps if I can see code. Thanx.
Mr. LightShiner
The only example I've found on overloading the subscript operator[] is this:
class foo{
public:
...
int operator[](int i);
int operator[](int i) const;
...
private:
int info[10];
};
int& foo :: operator[](int i){
return info;<BR>}<P>int foo :: operator[](int i) const{<BR> return info;<BR>}<P>But this doesn't help me because my private member data is not an array. The const non-referenced one can only be on the rhs of an assignment operator=, and the referenced one returns the same thing wether it is on the lhs or rhs of an assignment operator=. But I need to return an int on the rhs, and a foo& on the lhs.<p>[This message has been edited by Mr. LightShiner (edited October 28, 1999).]
I considered going into a long-winded dissertation on operator overloading and how to do it, but reconsidered and decided instead to send you here: http://www.codeguru.com/cpp/tic/tic0125.shtml
Read that chapter, check out the source code, you'll feel much better.
-fel
~Operator overloading is a Very Cool Thing~
Reason for doing this:
Size of Bool[8] = 32
Size of BoolArray = 1
My class looks like this:
typedef enum{ FALSE, TRUE } Bool;
// I have an older compiler that doesn't recognize bool
class BoolArray{
public:
...
void SetBit( int x, Bool y );
Bool GetBit( int x );
...
Bool& operator[]( int i );
BoolArray& operator= (??????)
...
private:
unsigned char info;
};
Bool& BoolArray :: operator[]( int i ){
return ????????;
}
BoolArray& BoolArray :: operator= ( ??? ){
????????
}
I want to be able to do this:
BoolArray p;
Bool x;
int i;
{I think that the return value of operator[](int i) should be:
return GetBit(i);
Which works fine if its on the rhs, but not so good on the lhs, this is where I'm confused.}
x = p;<BR>p = TRUE;<P>As it is now, I have to do this:<P>x = p.GetBit(i);<BR>p.SetBit(i,TRUE);<P>All I need is someone to help me fill in the question marks.
#define BIT_ONE 1;
#define BIT_TWO 2;
#define BIT_THREE 4;
#define BIT_FOUR 8;
etc...
If you're using the array for flags, you can give logical names (IS_DEAD instead of BIT_ONE) and have the code make alot more intuitive sense.
x = p.getValue(IS_DEAD);
p.setOn(IS_DEAD);
p.setOff(IS_DEAD);
it's especially helpful when you're doing an if statement.
if( p.getValue(IS_DEAD) ){...}
instead of...
x = p[2];
if(x) { ... }
which doesn't really tell you anything. You can also use the defined flags for masks in the class.
void
boolarray::setOn(int mask)
{
private_value = private_value | mask;
}
and you can also set multiple bools at once
p.setOn(BIT_ONE | BIT_FOUR);
If there's a specific reason you need to use an array, I'm a bit curious as to what it is.
/Niels
active / inactive
visible / invisible
moveable / not moveable
#define ACTIVE 0
#define VISIBLE 1
#define MOVEABLE 2
BoolArray properties;
// withot operator[] overloaded
properties.SetBit(ACTIVE,TRUE);
properties.SetBit(VISIBLE,TRUE);
properties.SetBit(MOVEABLE,FALSE);
// with operator[] overloaded
properties[ACTIVE] = TRUE;
properties[VISIBLE] = TRUE;
properties[MOVEABLE] = FALSE;
Still seem quite clear to me, even a little easier to understand.
Yup! You can easily find examples where operator overloading looks nice (Matrix math springs to mind), but it's equally easy to find example of horrible looking code.
My point is that if C++ didn't allow operator overloading, you'd know that a code piece like a[1]=2; is referencing an array, you don't have to worry that something spookey is going on behind the scenes. In c++, you never know, and that bugs me.
/Niels