Click to See Complete Forum and Search --> : Inheritance with C


BoSCHoW
April 7th, 2008, 02:14 AM
Hello,

it is possible to make some cind of inheritance with c using structs and unions?

Best regards,
BoSCHoW.

S_M_A
April 7th, 2008, 02:55 AM
As far as I know the very first C++ compiler was actually a C pre-processor so it can be done but I can't say to what extent it is practical.

If you think of a module (c-file) handling a specific struct (or rather a pointer to a struct) as a class then this class can be extended with a second module that reuses the functionality in the first module. This inheritance would not be as seamless as in C++ so to overcome this you probably want to declare a new struct for every "inheritance" you make (sooner or later the maintainance work will probably be a pain in the...). There will also be a need for a lot of static casting adding up to the maintainance.

Just a thought, maybe a C++ pre-processor still can be found/bought? Have you searched for instance sourceforge?

_uj
April 7th, 2008, 03:11 AM
The best of course is to use an OO language but if you for some reason cannot, this article maybe can give you some hints,

http://www.embedded.com/97/fe29712.htm

Lindley
April 7th, 2008, 03:35 AM
It's definitely possible, GTK+ does it.

I get the impression that it's accomplished there by requiring the coder to cast to the base type that actually contains the desired functionality; but the casting is done using a macro that includes some sort of validity check. I don't know the details, but the system seems to work pretty well.

Zaccheus
April 7th, 2008, 04:50 AM
What do you mean by inheritance.

One thing you can do is simply use composition:


struct X
{
int a;
};

struct Y
{
struct X x;
int b;
};

void f(struct X* x)
{
}

void g(struct Y* y)
{
f( &(y->x) );
}

JohnW@Wessex
April 7th, 2008, 08:37 AM
You could always have a base structure that contained pointers to functions. Each new 'derived' structure could be created by assigning different functions addresses to these pointers, thereby creating function overrides. There could be several 'creator' functions whose job it was to assign particular sets of functions to a structure.

pm_kirkham
April 7th, 2008, 05:36 PM
Pretty well any virtual machine for an object oriented language will implement inheritance using C.

Vtables are not the only dispatch mechanism, you can create big switch statements, or combine vtables with a couple of cached cases at the dispatch site.

How the type is encoded varies from vtable pointer, pointer to a type struct in the struct, or implicit schemes, such as allocating different small objects from different heap pages and manipulating the pointer to get the type stored at the heap base (eg, with 4K pages, ((type_t*)((size_t)p)&~(size_t)0xfff)) would give the type data for p, unless p == p &~0xfff in which case p points to a type).

Zaccheus
April 8th, 2008, 02:47 AM
Regarding virtual functions, this article called "COM in plain C" might help : http://www.codeproject.com/KB/COM/com_in_c1.aspx