Re: Base class and OOP in C

From: Måns Rullgård (mru_at_inprovide.com)
Date: 03/29/05


Date: Tue, 29 Mar 2005 17:19:52 +0200

j_mckitrick@bigfoot.com writes:

> Hi all,
>
> I am trying to implement some C++ functionality in C.
>
> Suppose I have several libraries that share behavior and properties,
> such as version, working directory, and so on. I want other
> libraries to extend this behavior without having to duplicate any
> code. For instance:
>
> typedef struct base
> {
> char version[32];
> char path[32];
> } base_t;
>
> typedef struct foo
> {
> base_t *base;
> int data;
> } foo_t;
>
> typedef struct bar
> {
> base_t *base;
> int data;
> } bar_t;
>
> I want the derived modules to link with the base library, so that my
> application can link with only the derived modules and still be able
> to call the base functions without having to link to that library or
> call 'wrapper' functions in the derived modules.
>
> Is there a way to do this?

If you change your declarations slightly, it will work:

typedef struct base {
    whatever;
} base_t;

typedef struct foo {
    base_t base; /* not a pointer */
    foo_additions;
} foo_t;

typedef struct bar {
    base_t base;
    bar_additions;
} bar_t;

Now, a pointer to any of these, will be valid as a base_t *.

-- 
Måns Rullgård
mru@inprovide.com


Relevant Pages

  • Re: Base class and OOP in C
    ... > Suppose I have several libraries that share behavior and properties, ... > typedef struct base ... > I want the derived modules to link with the base library, ...
    (comp.lang.c)
  • Base class and OOP in C
    ... Suppose I have several libraries that share behavior and properties, ... version, working directory, and so on. ... typedef struct base ... I want the derived modules to link with the base library, ...
    (comp.lang.c)
  • Base class and OOP in C
    ... Suppose I have several libraries that share behavior and properties, ... version, working directory, and so on. ... typedef struct base ... I want the derived modules to link with the base library, ...
    (comp.unix.programmer)
  • Re: use of backward single quote in procedure names, was: DST (summer time) offset
    ... char *dlistFile; ... thing that gets you beyond the standard C libraries. ... topic (RISC OS group, RISC OS language discussion) and I'll bother to ...
    (comp.sys.acorn.programmer)
  • Re: Base class and OOP in C
    ... > typedef struct base { ... > typedef struct foo { ... structure by using a pointer which can be resolved later. ...
    (comp.unix.programmer)