|
-
April 7th, 2008, 02:14 AM
#1
Inheritance with C
Hello,
it is possible to make some cind of inheritance with c using structs and unions?
Best regards,
BoSCHoW.
-
April 7th, 2008, 02:55 AM
#2
Re: Inheritance with C
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?
Last edited by S_M_A; April 7th, 2008 at 03:05 AM.
-
April 7th, 2008, 03:11 AM
#3
Re: Inheritance with C
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
-
April 7th, 2008, 03:35 AM
#4
Re: Inheritance with C
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.
-
April 7th, 2008, 04:50 AM
#5
Re: Inheritance with C
What do you mean by inheritance.
One thing you can do is simply use composition:
Code:
struct X
{
int a;
};
struct Y
{
struct X x;
int b;
};
void f(struct X* x)
{
}
void g(struct Y* y)
{
f( &(y->x) );
}
-
April 7th, 2008, 08:37 AM
#6
Re: Inheritance with C
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.
-
April 7th, 2008, 05:36 PM
#7
Re: Inheritance with C
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).
-
April 8th, 2008, 02:47 AM
#8
Re: Inheritance with C
Regarding virtual functions, this article called "COM in plain C" might help : http://www.codeproject.com/KB/COM/com_in_c1.aspx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|