Possible Duplicate:
Typedef pointers a good idea?
I’ve seen this oddity in many APIs I have used:
typedef type_t *TYPE;
My point is that declaring a variable of type TYPE will not make it clear that in fact a pointer is declared.
Do you, like me, think that this brings a lot of confusion? Is this meant to enforce encapsulation, or there are other reasons as well? Do you consider this to be a bad practice?
In general, it’s a bad practice. The significant problem is that it does not play well with
const:In order for the author of
foo()to express what they really mean, the library that providesTYPEmust also provideCONST_TYPE:so that
foo()can have the signaturevoid foo(CONST_TYPE mytype), and at this point we have descended into farce.Hence a rule of thumb:
If the definition of the underlying struct is not to be publicly available (which is often laudable), then that encapsulation should be supplied by the struct being incomplete, rather than by inconvenient typedefs: