Re: Question about a struct declaration



Andrey Tarasevich said:

Richard Heathfield wrote:
...
Er... I disagree.

Well, then, can you come up with a different implementation hiding
technique that would satisfy the following two requirements:

1) Client code can define objects of type 'T'
2) Client code doesn't see the implementation details of the concept
type 'T' represents
?

Yes, by carefully interpreting your words in a way convenient to myself. I
choose to interpret 'define' as meaning 'create via a function' (which is
completely at odds with our usual meaning of 'define', as you don't have
to remind me), and I choose not to count structness as an implementation
detail. Given those weasel words, I can do it:

Interface:

struct T_;

typedef struct T_ T;

T *t_create(whatever);
void t_destroy(T **);
int t_use(T *, whatever);
etc

Implementation: hidden :-)

Hide the pointeriness? No, it actually _doesn't_ hide the pointeriness.
The pointeriness will be clearly visible in the interface header, in the
typedef.

Right, so you gain nothing in terms of information hiding, but lose the
convenience to the user-programmer of being reminded through usage that
he's dealing with a here-be-dragons pointer.

a pointer
hidden in a typedef is a standard, widely used and accepted idiom.
Yeah, well, I've only got one pair of 'ands, ain't I?

I don't know what to say...

Translation: it's going to take me a long time to persuade everyone /not/
to accept that idiom, working alone. (Especially as I'm not actually all
that fussed about it, certainly not enough to commit my declining years to
a one-man anti-hiding-pointers-in-typedefs crusade.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • Re: Forward Declarations
    ... I didn't realize what a sin it was to use 'typedef'. ... C has no concept of "sin" other than the trigonometric function. ... If client code should view the type as opaque, ... If client code should view a struct transparently, ...
    (comp.lang.c)
  • Re: Typedef and const
    ... but using the typedef instead of struct my_obj? ... functions take pointer arguments. ... if client code never has to save a "read only" ...
    (comp.lang.c)
  • [bug] typedefed base class not visible [VC8 RTM.050727-4200]
    ... Most of it boils down to the following new compiler bug: ... typedef B::base base; ... but I'd really hate to need to refactor ALL client code.. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: krb524d and KRB524_KRB4_DISABLED
    ... the request and always return an error ... client code). ... ap = struct { ... This kinit was compiled against krb5-1.2.6 which seems to know only ...
    (comp.protocols.kerberos)
  • Re: Structure as object property
    ... Your struct must be declared at the same accessibility level as the ... meaning that no client code could ever instantiate a ... (Note, however, that client code _could declare_ an Address, ... minor efficiency improvements that might result. ...
    (microsoft.public.dotnet.languages.csharp)