-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?

7
  • 2
    Are you asking about something like this? Commented Dec 20, 2013 at 7: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. Commented Dec 20, 2013 at 7: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. Commented Dec 20, 2013 at 7:56
  • @XORcist C with classes and this and that and other fancy stuff, that would be C++. But both are considered different languages. Commented Dec 20, 2013 at 8: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: lwn.net/Articles/444910 Commented Dec 20, 2013 at 8:02

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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 :-(.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.