hey dwurmfeld,
could you please give an example of the scenario you've mentioned?
Sure, here is a piece of nonsense code, using a formal CoordinateClass would make more sense, but from the point of view of the argument, the union of structures doesn't much help "save space", indeed, if this were an embedded application, could you really trust the sizeof operator?
-
#include <cstdlib>
-
#include <iostream>
-
-
using namespace std;
-
-
int main(int argc, char *argv[])
-
{
-
typedef struct {
-
double x;
-
double y;
-
int imaginary;
-
} TComplex;
-
-
typedef struct {
-
double x;
-
double y;
-
} TwoD;
-
-
typedef struct {
-
TwoD base2D;
-
double z;
-
} ThreeD;
-
-
union UniversalCoordinate{
-
TComplex CoordComplex;
-
TwoD Coord2D;
-
ThreeD Coord3D;
-
};
-
-
// use the union
-
UniversalCoordinate fred;
-
fred.CoordComplex.x = 0.0;
-
fred.CoordComplex.y = 3.14;
-
fred.CoordComplex.imaginary = -3;
-
fred.Coord2D.x = fred.CoordComplex.x;
-
fred.Coord2D.y = fred.CoordComplex.y;
-
fred.Coord3D.base2D.x = fred.CoordComplex.x;
-
-
cout << "The Ycoordinate is: " << fred.Coord3D.base2D.y << endl;
-
system("PAUSE");
-
-
return EXIT_SUCCESS;
-
}
-