ImpalerCore a écrit :
> Personally I think implementing some public/private separation is
> outside the scope of C. Grafting this OO paradigm into C is purely
> arbitrary at best, confusing and complex at worst. C wants people to
> get their hands dirty, and doesn't do a whole lot to protect them.
>
Well, as you may have noticed, I proposed a common iterator object for
iterating through ANY container in my library. Obviously that object
can't be a SINGLE type, it must be several since it must have the specific
knowledge of each container, AND it must offer a COMMON interface, so user
code is independent of the container.
It is not just "a matter of protecting people from themselves", it is
just that is impossible to make a COMMON type for all container iterators
if each iterator exposes its interface.
The solution is then, to define a common interface with a few
function pointers that is shared by all containers. User code uses that
interface
typedef struct _iterator {
void (*Getnext)(structb_iterator *);
// Some other public function pointers
char private[]; // Document that this is a variable length structure
} Iterator;
In the file of the container implementation (say list.c) I
define
typedef struct tagListIterator {
Iterator it;
// Private fields follow
List *list;
unsigned timestamp;
// etc
} ListIterator;
Now, I can define
static void *GetNext(Iterator *it)
{
ListIterator *li = (ListIterator *)it;
// And now I can use the private fields
// in this implementation.
}
The advantage of this is that I can EXTEND a public structure with private
parts, something similar to simple inheritance in C.
> Again, you say a pure C solution is MUCH better, but I haven't seen a
> really cool use-case that says "Yeah, I want to try it out!".
When you write a library, it is very handy since it allows to present a common
interface for a set of different objects
> If I
> wanted to make something private in C, I'd just give the "private"
> members a hideously long name so that most people will just be annoyed/
> bored to type it out and use the "public" parts 
>
> Best regards,
> John D.
It is not so much a "private" vs public stuff but it is a thing of
interfacing different objects of different types through a common interface.