jacob navia said:
Using overloaded functions the switch disappears and you have:
void *overloaded print_structs(Type1 *p)
{ [snip]
void *overloaded print_structs(Type2 *p)
{ [snip]
"Polymorphic types"... can you explain?
Function overloading is merely a "syntactic sugar". If the OP wanted
that, he would probably have had:
void print_structs_Type1(Type1 *p);
void print_structs_Type2(Type1 *p);
which is not much more or less nuisance from declaring and invoking:
void print_structs(void *p, enum struct_type stype);
print_structs(pstruct, eType1);
etc.,
and he would complain he didn't like so many functions, how to have
only one.
Instead he had a *single* function, that handled many data types.
In his description he wrote he had a "generic print function", and
that's what he would probably like to stay with, since he only
wrote he didn't like to put implementation details into this
"generic function".
There's a huge difference between having many functions with the
same name, and a single function that can handle many types
of data.
What he probably wanted, and didn't know he wanted it, is the idea
of polymorphic type.
struct PolymorphicBase {
void (*print_myself)(struct PolymorphicBase*);
/*...*/
};
struct Type1 {
struct PolymorphicBase _pb;
/* ... Type1 data ... */
};
static
void printType1(struct PolymorphicBase *p_pb) {
struct Type1 *pT1 = (struct Type1*)p_pb;
printf("...", pT1->...);
}
void initType1(Type1 *pT1) {
pT1->_pb.print_myself = printType1;
/*...*/
}
void print_structs(void *generic) {
((struct PolymorphicBase*)generic)->print_myself(generic);
}
Each new TypeX defines its implementation in its own translation
unit, the implementation is hidden, and we have a single generic
function `print_structs'. Clean and dandy. Now we can build upon it:
typedef void (*generic_action_t)(void*);
generic_action_t tga[] = {print_structs, speak_structs, sing_structs,
play_structs, love_structs, hate_structs};
struct TypeN structN;
initTypeN(&structN);
tga[user_action_choice](&structN);
+++
By now I see thirty answers to your off-topic post,
and none (!) to the OP's problem. How helpful was that?
By the way, the C standard does have overloaded functions:
[snip]
So what? All operators are overloaded, too. So, what?
Why should this feature not be available to users?
It probably could. It could probably be even useful.
That's the whole point here.
The point is that you have brought it in an extremely
inapropriate way, hostile to the OP.
[ hurrah! finished! -- bed time... hrrr...]