Topic: typedef do not make real difference
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/03/30 Raw View
jack wrote:
>
> a simple example to illustrate my problem:
> typedef short table_hnd;
> typedef short record_hnd;
>
> void foo(table_hnd aTable,record_hnd aRec);
> table_hnd th;
> record_hnd rh;
>
> foo(th,rh); // this is correct
> foo(rh,th); // this is wrong,but compiler can not find it
>
> if foo has a lot of parameters,the error above is quite easy to
happen. but
> compiler can not help here because typedef do not make table_hnd and
> record_hnd two different types. the only way I can think of is to make
them
> a struct:
> struct table_hnd {
> short hnd;
> };
> struct record_hnd {
> short hnd;
> };
> but this is ugly. are there better ways?
No. You've got the right idea.
In addition to being ugly, it's incomplete. You also need to overload
lots of standard operators to use table_hnd and record_hnd in convenient
ways. It would save a lot of typing if the language allowed:
struct table_hnd : public short { /* implementation */ };
The fact that you can't do things like that is one reason people say
that C++ is not a truly object oriented language.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]