Question

if (vector1.x > ((float*)&vector1)[j])

Is j simply just an index into the vector?
e.g. is C++ able to retrieve these values using array notation even though vector isn't an array?
If so I'm guessing it achieves this by referencing vector by its address?

Was it helpful?

Solution

The C++ Standard says that casting a (pods) struct's pointer to a pointer of the type of its first element will yield a pointer to its first element:

struct a {
    float a1;
    float a2;
    float a3;
};

a o1; ((float*)&o1); // points to o1.a1

I suspect that code told the compiler not to add any padding between a1, a2 and a3, so that if it indexes the pointer, it will pointer exactly to the float it wants. So above

((float*)&o1)[1] // *would* return o1.a2

That's platform dependent, as padding can't be changed in Standard C++. Lookup how that code arranges it, and whether i'm right at all :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top