Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do inheritance from an abstract/base struct or simulate something along those lines in C?

I am currently working with a C program that uses structs composed of xyz coordinates, but sometimes these coordinate may refer to vectors (Force/velocity type, not the data structure) while at other times it may be referring to position. I know its possible to simply use one struct for all of these different conditions since they all use mostly the same data type (float), but to simply keep my math better organized (plus variable and struct names) and keep things from getting mixed up, is there a way to define a base struct that defines itself as having three floats, be inherited by another struct that more specifically defines what the struct is supposed to be (such as position instead of velocity, etc. etc.)? I know C is not OOP, but it seems like it could be possible to do this Here is what the base struct would be like:

struct ThreeDCartesianData
{
   float x;
   float y;
   float z;
};

A more specific struct would inherit from that and perhaps define extra variables, or use different names for the variables. There will be multiple position structs being used, but I think only one velocity struct for each set of data. I have seen similar questions to this, but they all seem to be referring to a higher level language (C++, C#, etc. etc.)

like image 552
cluemein Avatar asked Mar 14 '23 14:03

cluemein


1 Answers

You can use a union for this. Your main struct would contain a union of the "derived" structs as well as a "flag" field telling you which member of the union is valid:

enum { DERIVED11, DERIVED2, DERIVED3 };

struct derived1 { int x1; };
struct derived2 { char x2; };
struct derived3 { float x3; };

struct ThreeDCartesianData
{
   float x;
   float y;
   float z;
   int derivedType;
   union {
     struct derived1 d1;
     struct derived2 d2;
     struct derived3 d3;
   } derived;
};

Then you can use them like this:

struct ThreeDCartesianData data1;
data1.x=0;
data1.y=0;
data1.z=0;
data1.derivedType = DERIVED1;
data1.derived.d1.x1 = 4;

You could alternately define them like this:

struct common
{
   int type;
   float x;
   float y;
   float z;
};
struct derived1
{
   int type;
   float x;
   float y;
   float z;
   int x1;
};
struct derived2
{
   int type;
   float x;
   float y;
   float z;
   char x2;
};
struct derived3
{
   int type;
   float x;
   float y;
   float z;
   float x3;
};

union ThreeDCartesianData {
     struct common c;
     struct derived1 d1;
     struct derived2 d2;
     struct derived3 d3;
};    

And use them like this:

union ThreeDCartesianData data1;
data1.c.type=DERIVED1;
data1.d1.x=0;
data1.d1.y=0;
data1.d1.z=0;
data1.d1.x1 = 4;

If all the structs in a union have an initial list elements of the same type in the same order, the standard allows you to access those fields from any of the sub-structs safely.

like image 169
dbush Avatar answered Apr 07 '23 03:04

dbush