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

## Recommended Posts

I remember some time ago there was a thread on overloading operator[] to access class/struct members, so that, frex, one could do something like this:
struct Vec3
{
float x, y, z;
operator[] {...};
};

Vec3 myVec;
myVec[0] = 1.0f; //I just set myVec.x to 1.0f


I remember there being some fine points in getting this set up, but it's been quite a while since I read the thread, and I don't seem to have it bookmarked. If anybody could point me in the general direction of that thread, or a similarly helpful resource, I'd be most obliged. yckx

##### Share on other sites
I don't have the link, but wouldn't that be simple to implement? eg:

float& operator[](int i){    switch(i)    {        case 0: return x;        case 1: return y;        case 2: return z;        default: assert(false);    }}

Or if you wanted to use addresses:
float& operator[](size_t i){    assert(i < 3);    return static_cast<float*>(this);}

You may want to use (compile time?) asserts or something to check that your offsets are correct when using the address method.

##### Share on other sites
One way:

struct vec3{   union   {      float x, y, z;      float _m[3];   }}

or

float& operator[](int i){   switch(i)   {      case 0: return x; break;      case 1: return y; break;      case 2: return z; break;      default: //throw error?   }}

##### Share on other sites
Quote:
 union{ float x, y, z; float _m[3];}

isn't that different from a direct array operator? Correct me if I'm wrong, but the above would give:
vec._m[0]

as opposed to:
vec[0]

right?

##### Share on other sites
Yeah, but there was a rather involved discussion on some intricacy or other that I couldn't remember, and I wanted to read up on it.

##### Share on other sites
Quote:
Original post by CraZeE
Quote:
 union{ float x, y, z; float _m[3];}

isn't that different from a direct array operator? Correct me if I'm wrong, but the above would give:
vec._m[0]

as opposed to:
vec[0]

right?

You are definitely right...I just was throwing out another option.

##### Share on other sites
Quote:
Original post by visage
Quote:
Original post by CraZeE
Quote:
 union{ float x, y, z; float _m[3];}

isn't that different from a direct array operator? Correct me if I'm wrong, but the above would give:
vec._m[0]

as opposed to:
vec[0]

right?

You are definitely right...I just was throwing out another option.

It's also wrong. all three of those floats (x, y, z) will occupy the same memory location as _m[0].

##### Share on other sites
Quote:
Original post by CraZeE
Quote:
 union{ float x, y, z; float _m[3];}

isn't that different from a direct array operator? Correct me if I'm wrong, but the above would give:
vec._m[0]

as opposed to:
vec[0]

right?
Yeah, but then you could overload the [] operator to use that. For example:
float& operator[](int index) {    return _m;}

##### Share on other sites
Yep, you are right. This is what I meant:

struct vec3{   union   {      struct       {         float x, y, z;      }      float m[3];   }  }

Sorry.

##### Share on other sites
Quote:
 Original post by visageYep, you are right. This is what I meant:*** Source Snippet Removed ***Sorry.

Which uses a non-standard compiler extension.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 30
• 9
• 16
• 22
×

## Important Information

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!