Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Mr. LightShiner

Overloading operator[]; Need Some Light

This topic is 6987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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[i];
}

int foo :: operator[](int i) const{
return info[i];
}

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.

[This message has been edited by Mr. LightShiner (edited October 28, 1999).]

Share this post


Link to post
Share on other sites
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~

Share this post


Link to post
Share on other sites
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[i];
p[i] = TRUE;

As it is now, I have to do this:

x = p.GetBit(i);
p.SetBit(i,TRUE);

All I need is someone to help me fill in the question marks.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!