3

I have a class, for example:

class Vector
{
    float x, y, z
};
Vector v;

And a pointer:

float *c = &v.x;

Will it be works correctly, when I'll use increment operator for access to y and z members?

P.S. Bad style to do by this way, but it's sport interest.

fpohtmeh
  • 506
  • 4
  • 15

1 Answers1

1

Will it be works correctly, when I'll use increment operator for access to y and z members?

No. Undefined behavior. You can't perform pointer arithmetic across objects and you can't guarantee struct padding.

You can do stuff like this though:

class Vector
{
    float v[3];
    int& x() { return v[0]; }
    int  x() const { return v[0]; }
    // and so on ...
};
Pubby
  • 51,882
  • 13
  • 139
  • 180