3

Is it possible to inherit structure with another in standard C or C++?

Siddiqui
  • 7,662
  • 17
  • 81
  • 129

3 Answers3

13

You can embed a structure inside another to simulate inheritance in C:

typedef struct {
    int i;
} base;

void basefunc(base *b);

typedef struct {
    base b;
    char c;
} extended;

extended e;
/* Initialise extended here */
basefunc(&e.b);      /* Use the type checker */
basefunc((base*)&e); /* Just make sure you know what you're doing */
  • Yep. Though it leaves you with the awkward `e.b.i` syntax for accessing the "base" members. But that's just another reason to wrap all this up as a opaque type... – dmckee --- ex-moderator kitten Nov 07 '10 at 18:00
  • 3
    Or, since a pointer to a struct is guaranteed to also point to its first member, you can cast it - `basefunc ( ( base*) e )` – Pete Kirkham Nov 07 '10 at 18:07
  • @Pete Kirkham, thanks, I've added that to my answer with a warning. –  Nov 07 '10 at 18:13
  • You can have [anonymous structures](http://stackoverflow.com/questions/1972003/how-to-use-anonymous-structs-unions-in-c) which avoid the `e.b.i` syntax. – Ben Jackson Nov 07 '10 at 18:24
  • That's just as close as you can get. Still: e.g. the access to a member of struct base involves the necessarity of casting, there is no function /method overloading, you won't have methods, so you have to pass in a pointer to your struct every time you want to operate on the object represented by it. There are books on object - orientated programming with ANSI - C but they pretty much related to implementing a framework to simulate the features of C++ and can't resolve the described issues. – Peter Mar 25 '14 at 22:01
10

C does not support inheritance.

C++ does support inheritance.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • @James, can you kindly send me the reference?? – Siddiqui Nov 07 '10 at 17:53
  • 3
    @Arman: Sure. Stack Overflow has [a list of good beginner C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and [a list of good beginner C books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). Any of those beginner books would contain this information. – James McNellis Nov 07 '10 at 17:54
  • 1
    @Arman - uhmmm.... you should google for ANSI C Standard + wg14...your comment's undertones sounds like as if you don't believe his answer and that you're sceptical.... – t0mm13b Nov 07 '10 at 17:57
  • @tommieb75, I have accepted James answer, I am just asking for the reference for my own knowledge. – Siddiqui Nov 07 '10 at 18:00
  • @tommieb75, I usually ask the question on SO before google, because I got the right direction from other's experience. – Siddiqui Nov 07 '10 at 18:09
  • @Arman, here is another reference: http://www.planetpdf.com/codecuts/pdfs/ooc.pdf – Christoffer Nov 07 '10 at 18:26
  • 1
    @Arman: well I am so sorry to see SO being downgraded to asking questions that can easily be found out elsewhere or the fact that - it never dawned on you to google using the keywords standard c, which was the basis of this question, there's far too many low quality questions being asked by contributors like yourself who are expressing themselves lazily.... and asking others to do the work for them... – t0mm13b Nov 07 '10 at 19:05
  • @Arman C _does not and never will_ support inheritance. That is why the force was used and we have C++. You can `ad hoc` similar behaviour using function pointers, type safe callbacks and an overall egregious abuse of the language given the fact that C++ exists, but _why?_ You can grab enough from CCAN (hint, typesafe CB's, antithread, talloc and more) to pull this off in C, but again, WHY? – Tim Post Nov 07 '10 at 19:11
4

The only difference between struct and class is default visibility of members and default inheritance mode. struct D : B { ... is equivalent to class D : public B { public: ....

fredoverflow
  • 256,549
  • 94
  • 388
  • 662