• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

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

Mr. LightShiner

Overloading operator[]; Need Some Light

11 posts in this topic

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

Share this post


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

0

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

0

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~

0

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.

0

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.

0

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

0

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.

0

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

0

Share this post


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

0

Share this post


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

0

Share this post


Link to post
Share on other sites