Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Overloading operator[]; Need Some Light


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Mr. LightShiner   Members   -  Reputation: 122

Like
Likes
Like

Posted 30 October 1999 - 08:37 AM

I have a problem that I can't figure out and I'm in the dark. Can someone please shine some light in my direction. I want to overload the subscript operator [], but I need to call different functions depending on what side of an assignment operator = it is on. Example:

x = foo[i]; // call func1(i);

foo[i] = x; // call func2(i,x);

Mr. LightShiner


Sponsor:

#2 mason   Members   -  Reputation: 128

Like
Likes
Like

Posted 28 October 1999 - 05:54 AM

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


#3 logistix   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 October 1999 - 06:00 AM

I think you want to overload the = sign too, and have it return a tempFoo that foo[i] processes.

#4 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 28 October 1999 - 07:10 AM

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


#5 Mr. LightShiner   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 October 1999 - 08:00 AM

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


#6 felisandria   Members   -  Reputation: 739

Like
Likes
Like

Posted 28 October 1999 - 08:23 AM

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~


#7 Mr. LightShiner   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 October 1999 - 09:57 AM

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.


#8 logistix   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 October 1999 - 10:50 AM

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.



#9 Niels   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 October 1999 - 04:12 AM

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


#10 Mr. LightShiner   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 October 1999 - 10:19 AM

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.


#11 Niels   Members   -  Reputation: 122

Like
Likes
Like

Posted 30 October 1999 - 07:46 AM

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


#12 felisandria   Members   -  Reputation: 739

Like
Likes
Like

Posted 30 October 1999 - 08:37 AM

One thing to watch out for when overloading multiple operators and using them together:

C++ keeps the original order of operations for overloaded operators. This can be a really cool thing if you're overloading to, say, enhance the functionality of a data type (a situation in which one of my teammates used operator overloading to allow ranges of data to be compared during computation comes to mind) but if you weren't aware of it, you can get some bizarre things happening depending on how you set up your operators.

-fel
~ When in doubt, add ()'s. ~





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS