DragonFly BSD
DragonFly users List (threaded) for 2005-03
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

Re: 'Base class' shared libraries in C?

From: Dennis Melentyev <dmelentyev@xxxxxxxxx>
Date: Tue, 29 Mar 2005 17:19:11 +0300


Yes, if you'll not pretend it to be even a "small C++".

IMHO, the simplest way to get "overloaded methods" is to implement base class methods pointers table, some kind of derived "classes" initialization (registering overrided methods, setting base class pointers in children, etc). Actualy, it's the way it implemented in C++.

Anyway, you framework should take a BIG care of probability to call "pure virtual" analogs (uninitialized or not overloaded members).

In your particular case, using of multiple modules would be impossible or inconvenient, because of each module would contain the same code for "base class", confusing linker/rtld.

Isn't it easier to explicitly link to "base" module and at least one "derived" module?
Or, to use just normal C++?
The later will also give you a compiler driven "spellchecker" for free

Jonathon McKitrick wrote:
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;

	base_get_version(base_t *, char *);
	base_get_path(base_t *, char *);

	foo_one_method(foo_t *, int data);
	foo_two_method(foo_t *, int data);

	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?


Dennis Melentyev

[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]