"Jack Klein" <jackklein@spamcop.net> wrote in message
news:cb3p101eptut9bhnhttbjq3pcaul52gj4t@4ax.com...[color=blue]
> On 31 Jan 2004 18:16:31 -0800,
mike@odyne.com (Mike) wrote in
> comp.lang.c:
>[color=green]
> > Attached below is a file that implements polymorphism in C. It's
> > loosly based on the way Miro Samek describes it
> > (
http://www.embedded.com/97/fe29712.htm). I found the article a
> > little confusing so I made some changes and came up with wat's
> > below. It appears to work, but I've written plenty of programs before
> > that seemed to work, but didn't. Anyone willing to take a look, and
> > check for correcness? I would also love to hear suggestions on how to
> > simplify, improve upon, or increase the power of this method.[/color]
>
> If you want to simplify this method, use C++. Really. That's what it
> is for. If you wanted to expand your example by having more than one
> "member" function, you would need to either increase the size of each
> structure for more function pointers, or invent your own "vtable".
> Why do all the work when any C++ compiler, even a 10 year old one,
> will do it for you for free?
>
> Attempting to write C++ in C generally leads to results that are
> neither good C nor good C++. At least I haven't seen such an effort
> yet that was.[/color]
There are times when C++ is just not a good choice
compared with C. The polymorphic technique presented
is extremely old and it only supports single-inheritance.
However, it is quite viable if there is an automated way
of generating the rather complex supporting C code for
instance construction and destruction.
I use a very similar technique in a TCP/IP component
that defines each message packet as a "class". The complexity
of keeping everything in synch (which a C++ compiler would
do automatically) is handled by a program generator that
accepts XML input that specifies the message packet fields
and types, then spits out the appropriate supporting C
code (and Java) that implements the polymorphic code. Functions
are represented in their own namespace by using function pointer
fields within a struct. A supporting constructor function
is generated that allocates the memory for the instance
data and assigns the function pointers to the class struct.
Parent constructors/destructors are automatically called as
needed when an instance is created or destroyed. Similar
Java classes are also generated so that my application can
send the message packets between Java and C.
There is no way that I could possibly keep everything
organized in C (and Java) without automation. At the
"client level" where I invoke the polymorphic methods,
it is simply a matter of double indirection (i.e., two
dereferences via ->) to get the function pointer and
pass the parameters, including the required first parameter
that points to the object instance. The "client level"
usage is very simple and easy to use, so it pays off
with higher productivity.
I have a GUI front-end for the program generator. Adding
a new field to message packet, or creating new message
packets is just a point-and-click. All of the Java and
C code is regenerated (thousands of lines of code). I
recompile the application and it's ready to go.
I suggest reading "Object Oriented Software Construction"
(2nd Edition) by Bertrand Meyer, before getting too deep
into this polymorphic C thing. Thinking in object oriented
terms and understanding how object oriented semantics are
expressed and represented is vital before you get too
deep into the C implementation details.
2 cents worth. Your mileage may vary.
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!