Base class and OOP in C

J

j_mckitrick

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;

lib-base.so:
base_get_version(base_t *, char *);
base_get_path(base_t *, char *);

lib-derived-foo.so:
foo_one_method(foo_t *, int data);
foo_two_method(foo_t *, int data);

lib-derived-bar.so:
bar_one_method(bar_t *, int data);
bar_two_method(bar_t *, int data);

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

=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

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 *.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top