-1

I have seen that we can implement multiple inheritance in ANSI C? As we can create any class like

struct sampleClass {
    int size;
    struct sampleClass *ptr1;
};

As I tred to create class and want to implement inheritance. Can you give me some idea how to implement that?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
hims
  • 325
  • 3
  • 10
  • 2
    Are you asking about [something like this](http://stackoverflow.com/q/3322650/2065121)? – Roger Rowland Dec 20 '13 at 07:53
  • Of course it's possible to implement multiple inheritance in C. In fact, the very first C++ compiler created C code that was compiled by a normal C compiler. It's not that simple though. – Some programmer dude Dec 20 '13 at 07:54
  • C structs are not "classes" in the OOP sense. They are just collections of data fields, OOP "classes" should also have methods and support inheritance. You *can* implement just about anything in C including any OOP mechanism, but you have to do it yourself, in code, compiler will not do it for you, so result is going to be verbose and messy. – hyde Dec 20 '13 at 07:56
  • @XORcist C with classes and this and that and other fancy stuff, that would be C++. But both are considered different languages. – glglgl Dec 20 '13 at 08:00
  • 2
    Perhaps you should ought to start out with single inheritance? In event here is a pretty good write-up with a few implementation options and considerations: http://lwn.net/Articles/444910 – doynax Dec 20 '13 at 08:02
  • So, please clarify. Do you have an actual *specific task* to implement inheritance mechanism in C, on top of structs? If not, then switch language... A short list of languages, which have "real classes" but otherwise very C-like syntax and structure: C++, Objective-C, D, C#, Java – hyde Dec 20 '13 at 08:02
  • Regarding classes: object orientation doesn't concern itself with the implementation details of a class, as long as it has private encapsulation and possibility of inheritance. Other things such as constructors, destructors, this-pointers etc are convenient, but they are not related to OO design as such. A struct can have private encapsulation through incomplete/opaque type. Incomplete type can also be used for inheritance, as can struct pointers and function pointers. So you can do object-oriented programming in C, but you cannot have classes. – Lundin Dec 20 '13 at 08:06

2 Answers2

3

It depends what you mean by "implementing inheritance in C".

It has certainly been done with macros, coding guidelines and styles, and struct of function pointers (mimicking virtual method table) and other metadata. A good example comes from GTK with its GObject layer.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

I have seen this being done before.

#include "stdio.h"

#define FRUIT_COLOR_RED   1

struct fruit {
    char* name;
};

struct apple {
    struct fruit base;
    int color;
};

void printFruit(struct fruit *f) {
    printf(f->name);
}

int main(int argc, char** argv) {
    struct apple a;
    a.base.name = "apple";
    a.color = FRUIT_COLOR_RED;

    printFruit((struct fruit*)&a);

    return 0;
}

This works because struct fruit is the first member of struct apple and the two structures are aligned in memory.

|-- 4 or 8 bytes(char*) --| => struct fruit in memory

|-- 4 or 8 bytes(char*) --|-- 4 or 8 bytes(int) --| => struct apple in memory

The first 4 or 8 bytes(depending on 32 bit vs 64 bit respectively) are the same length and type so you can cast a struct apple* to a struct fruit* and because they line up in memory, it reads the members of struct fruit.

I don't know if this is good practice and I would avoid it. I love the simplicity of C, but why not use a language that has these constructs built in such as C++.

Too implement Object Orientation features in C is going to mean more lines of code and more maintenance.

Also if the code doesn't build I wrote it now in notepad, but the concept is there. Don't have a C compiler on me at the moment :-(.