Jump to content
  • Advertisement
Sign in to follow this  
yckx

overloading operator[] to access class members

This topic is 4881 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

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 this post


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


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by visage
Yep, you are right. This is what I meant:

*** Source Snippet Removed ***

Sorry.

Which uses a non-standard compiler extension.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!