#need help trying to figure out how to alias member variables as array

46 messages · Page 1 of 1 (latest)

upbeat forge
#

I have a Vector4 class I created, that has x, y, z, and w, however I also want to be able to index this class (vec[0] = x, vec[1] =y, etc.)

i can't figure out how to do this without using functions (vec.x()), however I really want to be able to use vec[0] and vec.x, without ()
i have also been told that overriding the [] operator and using a switch statement would be slower, and my code is performance critical. is there any way to do this? maybe with references?

old templeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

main axle
#

operator[]

fossil acorn
#

you open with describing code... stop it! just write the code and show it

upbeat forge
#

class Vec4{
public:
float x, y, z, w;

this is the current code, I just want to be able to index these variables without any performance loss

main axle
#

u dont need a switch for the operator btw

#

theres another way which idk if people will like but its through unions

fossil acorn
#

what does index mean? stop describing things with weird language, and write the code for what you want to do

main axle
#
class vec4 {
public:
  union {
    std::array<float, 4> arr{};
    struct {
      float x, y, z, w;
    };
  };

  float& operator[](size_t i) {
    return arr[i];
  }
};
upbeat forge
fossil acorn
#

so [0] means the 'x' member var

sturdy anchor
#

acc wouldnt work rename x to x_m

main axle
fossil acorn
#

i suppose [1] is the y

upbeat forge
sturdy anchor
#

oh then make x y z w public

main axle
#

i forgot that too in the codeblock

fossil acorn
#

is this v[runtime var] or v[compile time var] ?

upbeat forge
#

wym?

#

ohh

#

runtime, for loops mostly

#

float data[4];
float &x = data[0];
float &y = data[1];
float &z = data[2];
float &w = data[3];

would something like this work?

granite sparrow
main axle
main axle
#

its the only option, we dont have python/c# class attributes

#

and with a typesafe union u wont get .x .y u need to have wrappers

granite sparrow
#

So if you want your original goals in a portable non UB that's just impossible

#

If you want something with the right performance you use .x() and give the syntax you wanted

upbeat forge
granite sparrow
#

Or other similar alternatives

sturdy anchor
#

just use [] it isnt slower itll just get inlined by the compiler

granite sparrow
main axle
#

;compile -std=c++23 ```cpp
#include <array>
#include <print>

class vec4 {
public:
union {
std::array<float, 4> arr{};
struct {
float x, y, z, w;
};
};

float& operator[](size_t i) {
return arr[i];
}
};

int main() {
vec4 v{ 1, 2, 3, 4 };
std::println("{}, {}, {}, {}", v.x, v.y, v.z, v.w);
std::println("{}, {}, {}, {}", v[0], v[1], v[2], v[3]);
}

quiet troutBOT
#
Program Output
1, 2, 3, 4
1, 2, 3, 4
granite sparrow
#

References as data member break a lot of things

main axle
#

oops

granite sparrow
#

And basically your whole thing is now storing an extra 4 "pointers"

main axle
#

^

#

its either u go the ub route or u keep an internal std::array and implement .x() .y()

upbeat forge
#

ok, thanks. I will just be forced to use x() instead of x, thanks

#

!solved

old templeBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

sand parrot
#

and have regular x, y, z, etc. members