Jump to content
  • Advertisement
Sign in to follow this  
Not Registered

c++ variables.

This topic is 4668 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 have got this class class Vector { public: int Data[3]; } but i dont want to use Data[0] Data[1] Data[2] for my x,y,z. What i need is something like different name for Data[0] ... etc. something like (typedef Data[0] x;) but for variables. I want to use my vector class like this Vector.x == Vector.Data[0] And this will be EXACTLY the same as Vector.x == Vector.x or Vector.Data[0] == Vector.Data[0]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Why does it have to be in an array?

Why can't you just do this:

struct Vector
{
int x,y,z;
}

?

Share this post


Link to post
Share on other sites
Try this:


class Vector
{
public:
union
{
struct
{
int x;
int y;
int z;
};
int Data[3];
};
};

It uses anonymouse structs and unions. I don't know if this works in other compilers than VC++ though.

Share this post


Link to post
Share on other sites
why not just



class Vector
{
public:
int x,y,z;
};



im pretty sure you could even cast between the two classes safely if you needed to...

Share this post


Link to post
Share on other sites
That notation doesn't exist, however, there's a fairly ugly hack that will allow you to use operator[] to index variable names.

class Vector {
public:
int x;
int y;
int z;

int & operator[](int index) {
return this->*vec[index];
}

private:
typedef int Vector::* const Indirector;
typedef Indirector IndirectionArray[3];
static const IndirectionArray vec;
};

Vector::IndirectionArray Vector::vec = { &Vector::x, &Vector::y, &Vector::z };

Share this post


Link to post
Share on other sites
Wow, if you call that an ugly hack I don't know what qualifies as a nice hack.

Anyway, after reading the other thread I'll still be amazed if all compilers can optimize that as well as this, even-uglier and totally non-standard-conformant hack:


struct Vector
{
int x, y, z;
int& operator [] (int index)
{
return *((&x)[index]);
}
};



That will work as long as &y==&x+1 and &z==&x+2, but I wouldn't advise accessing the vector using both methods in the same function (and that includes functions that could be inlined in other functions) in case optimization aliasing rules cause the compiler not to recognise they're the same (I don't know exactly what the standard says on this subject).

And come to think of it, here's a different hack which might also work well, depending on your requirements.


struct Vector
{
struct VectorAccessor
{
int &x, &y, &z;
};

int Data[3];

VectorAccessor operator -> ()
{
VectorAccessor va;
va.x = Data[0];
va.y = Data[1];
va.z = Data[2];
return va;
}
};



Then you have to access x, y, and z via vector->x instead of vector.x.

Share this post


Link to post
Share on other sites
Quote:
Original post by ZQJ
Wow, if you call that an ugly hack I don't know what qualifies as a nice hack.

Anything that has to deal with pointers to members is ugly. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by LilBudyWizer
Or you could:

*** Source Snippet Removed ***


If you don't care about the size of your class.

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!