Here’s a question I posted at The Scripts Forum:
I have a class which does coordinate conversions.
I have several different data structures which are used to pass data in and out of this class.
I have a setCoordinate function which I use to pass one of these data structures in, to set the input coordinates.
Here’s my problem:
How do I define these data structures? Should they be a class or a struct?
They need to have a base class in order to be passed into the set coordinate function. The base class doesn’t have any parameters or functions for the derived data structures to inherit.
XXX Base:{};
XXX A: Base
{
int x;
int y;
}
XXX B: Base
{
double la;
double lo;
}
setCoordinates( Base* ba );
Next question: Should these data structures be included in the header file where setCoordinates is defined or should each one have its own header file.
If each should have it’s own header file, then it should be a class, but should they be classes?
If this is an object oriented design, then it seems like they should be classes, but all they do is pass data in and out of the conversion class.
Unfortunately, I did not receive a response.
I poked around the web a bit and came across this link. http://java.sun.com/developer/Books/shiftintojava/page1.html, the Replace Unions with Class Hierarchies section. In C, this tagged union method makes sense. However, I’m programming in C++. C++ is an object oriented programming language. Tagged unions are not object oriented. Classes are object oriented. From what I’ve read structs only exist in C++ for backwards compatibilty with C.
Anyways, here’s what I ended up doing:
typedef struct {
int tag;
union U
{
struct A
{
int x;
int y;
}a;
struct B
{
doublela;
double lo;
}b;
circleDimensions_t circle;
} u;
} Base;
Tagged unions in C++ condense things, but I don’t like it.
Leave a Reply
You must be logged in to post a comment.