Overloading operator[]; Need Some Light

Started by
10 comments, last by Mr. LightShiner 24 years, 6 months ago
If you overload the []'s to return a reference, you should be good to go in both cases.

Mason McCuskey
Spin Studios
www.spin-studios.com

Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
Advertisement
I think you want to overload the = sign too, and have it return a tempFoo that foo processes.
-the logistical one-http://members.bellatlantic.net/~olsongt
mason, logistix:

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

I'm registered now!

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).]

You want to overload operator =, not [], unless "foo" is a function.
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~

~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Actually, I believe that I want to overload both the operator= and operator[]. I am simulating an array of eight boolean values using the bitwise & and | operations on an unsigned char.
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.

Actually, the way you're doing it now is probably a better way to do it than all that crazy overloading. You might just be thinking too much. I'd keep it like that and then do some defines:

#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.

-the logistical one-http://members.bellatlantic.net/~olsongt
I strongly oppose of operator overloading. I've seen so many times where the meaning become VERY unclear. A good old fashioned function (or a macro in the very simple cases)... A name such as a.getBit(8) generally says a lot more than a[8].

/Niels

<b>/NJ</b>
Let's say I have an object has a few properties:
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.

mhm!

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

<b>/NJ</b>

This topic is closed to new replies.

Advertisement