I have a simple point struct
that I would like to inherit:
struct PointXYZ
{
double x, y, z;
};
class extendedPoint : public PointXYZ
{
public:
double getXYZ(int i);
void setXYZ(int i, double value);
private:
int moreproperties;
};
The get-set function is indexed because I'd like to be able to loop through getting and setting the xyz values. I'd like to link the concept of indexed xyz and standalone x,y,z by modifying my base class as described in the answer to this post:
struct PointXYZ
{
union {
struct {
double x, y, z;
};
double xyz[3];
};
};
class extendedPoint : public PointXYZ
{
public:
double getXYZ(int i) { return xyz[i]; }
void setXYZ(int i, double value) { xyz[i] = value; }
private:
int moreproperties;
};
But this post directly contradicts the first post on whether the following is valid:
double dostuff()
{
PointXYZ p;
p.x = 123.88;
return p.xyz[0];
}
So, is PointXYZ
's use of union
valid c++ and consistent between compilers or not?