Alex said:
What I want to do is something like Interface in Java or
abstract class in C++.
In the struct s1, I will declare some function pointers, which
I will not assign for them. And in struct s2, I will delcare a
object of struct s1, then assign for the function pointers of
the object. The intention of doing all of above is to implement
simple OOP using C.( Because In my enviroment, I cannot use
other language.)
So, if you can provide any information , even a link, about how
to implement simple OOP, I will appreciate your help very much.
There have been several discussions about this in comp.lang.c;
maybe you can find something useful searching the archives:
<
http://groups.google.de/groups?as_q...s_miny=2004&as_maxd=17&as_maxm=4&as_maxy=2006>
The basic technique of "typesafe casting", always remains the
same: You have a pointer to a structure; you know that the
first member of this structure always is or contains (maybe
over several levels) a structure describing a "parent class".
You always can cast a pointer of a "child class" to a pointer
of the "parent class". This of course restricts you to single
inheritance.
Often, all "classes" are derived from one generic "class" which
may even offer a way to find out the "type" of the "class" it
is a member of. In this case, it is safe to cast "from small to
large", i.e. from the pointer of the "parent class" to a pointer
to the "child class"; this can become the source of merry
confusion if not done right.
Cheers
Michael