tags:

views:

79

answers:

2

Hi

To make a struct's members "private" to the outside I know that I can do this.

In the .h file

typedef struct Obj Obj;

In the .c file you then

struct Obj {
int a;
int b;
}

This will keep the knowledge of the existense of a and b from beeing known. But all the "member" functions in the .c file will now about them and can opperate on then. But what I wonder about now is that if you can make PART of the struct "private". Say that I want to keep those a and b variables "private" but then I want to have some function pointers that I want to remain public. Is this possible? I know that if I try to declare these in the struct in the .h file I would get a duplicate declaration error on the struct.

+2  A: 
Pete Wilson
Was thinking about the same thing, having a struct with the functionpointers... I'll give it a try and see.
inquam
Just remember to pass an instance of struct Obj to your functions when they're called. That, or you could implement your own currying in C.
Nathon
+2  A: 

Copy Python: use the honour system for ‘private’ members.

Put a _ at the start of the member name as a red flag that other code shouldn't be touching it. Unless you've got security boundaries inside your application (which is uncommon), you don't actually need real privates, you only need a convention agreed-upon by members of your team to pretend they're private.

Try not to add complexity by forcing C into a paradigm which it doesn't really fit. Extra headers for public/private interfaces and extra source files for class management are going to make your development work more complicated, defeating the ‘more maintainable’ goal that information-hiding is aiming for.

bobince
That would work great. But here it's a question of different type of objects that might all look totaly different, but all have in common that they have a run function. That function always take the same arguments but can have totaly different implementations. Thus it would make sense to be able to only expose that run function and the letting the individual objects handle how and with what they "run". But you are right, it's a possibility and if nothing else comes up I might very well go that route. Thanx.
inquam