typedef vs inheritance

C

Carmen Sei

I found in C++ a type is typedef several times and alot of time, I
need to dig further to see the original type.

like LPDWORD - it's a strange type, but when i dig further, i see it's
a (DWORD *)

===========
typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;

===========
i feel that creating new type in C++ by using "typedef" is similar to
inheritance in Java -

where alot of time I need to dig further to found the "PARENT" type of
an object hierarchy.
 
I

Ian Collins

Carmen said:
I found in C++ a type is typedef several times and alot of time, I
need to dig further to see the original type.
You or Eric?
like LPDWORD - it's a strange type, but when i dig further, i see it's
a (DWORD *)
So what's a DWORD?
===========
typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;

===========
i feel that creating new type in C++ by using "typedef" is similar to
inheritance in Java -
A typedef is an alias, not a new type.
 
M

Martin York

I found in C++ a type is typedef several times and alot of time, I
need to dig further to see the original type.

like LPDWORD - it's a strange type, but when i dig further, i see it's
a (DWORD *)

===========
typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;

===========
i feel that creating new type in C++ by using "typedef" is similar to
inheritance in Java -

where alot of time I need to dig further to found the "PARENT" type of
an object hierarchy.


Do you really need to dig:
MS uses some name conventions:
LPDWORD -> L(ong)P(ointer)D(ouble)WORD
PDWORD -> P(ointer)D(ouble)WORD

But a typedef is just an alias not very much like inheritance.
 
I

Ivan Vecerina

:I found in C++ a type is typedef several times and alot of time, I
: need to dig further to see the original type.
:
: like LPDWORD - it's a strange type, but when i dig further, i see it's
: a (DWORD *)

What you are observing is an idiosyncrasy of Windows headers,
not typical or recommended C++ style.

Most C++ experts will prefer to avoid declaring typedefs for
something as simple as a pointer. But there are some platform-
specific historical reasons for the use of some typedefs on
Windows, so you will see them used there...

: typedef DWORD near *PDWORD;
: typedef DWORD far *LPDWORD;
NB: near & far are non-standard C++ extensions that were
useful in 16-bit x86 times.

: i feel that creating new type in C++ by using "typedef" is similar to
: inheritance in Java -
It is not a new type, but another name for the exact same thing.

: where alot of time I need to dig further to found the "PARENT" type of
: an object hierarchy.
Indeed. This is why this style is not recommended. A new name
only should be introduced when there is a valid reason to.

Typedefs are normally only used (1) when there is a suspicion that
the underlying type might vary (e.g. on different platforms/target),
(2) when it provides a useful form of encapsulation (e.g. a class
containing a typedef that will be used by its clients), or (3) to
provide a shortcut for a complex type (in which case it is often
kept local, declared within the function where it is used).
 
C

Carmen Sei

Then are you meaning

the following will compile only on Microsoft compiler but not other
C++ compiler?

typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;

It's compiler specific added syntax (near / far) ??
 
J

James Kanze

Then are you meaning
the following will compile only on Microsoft compiler but not other
C++ compiler?
typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;
It's compiler specific added syntax (near / far) ??

It's definitely compiler specific added syntax, not standard,
and will not compile on any of the Unix compilers I use. As
Ivan said, it's a platform specific extension, and not part of
the standard language. It's an extension which addressed a very
real problem on 16 bit Intel processors, however, which was
present (in one form or another) in all compilers for that
platform, and could very well be present in their descendents on
Windows today (for reasons of backward compatibility). It
wouldn't surprise me to see it in the Borland compiler as well,
for example.

I'd also like to expond on Ivan's point about not abusing
typedef's. A typedef results in information hiding. A good
thing, if the information is irrelevant to the user; a bad thing
otherwise. Something like:
typedef int WidgitCount ;
, for example, is good: all I need to know about WidgitCount is
that it is a counter (which the name tells me)---perhaps on
another platform, or in the future, it will be long, or even
BigInt. Typedef's like the above, however, hide the fact that
you're dealing with a pointer. The fact that you're dealing
with a pointer, however, is important information---you don't
use a pointer like you'd use the value itself. (If I'm not
mistaken, DWORD is also a typedef. And also equally abusive.)

Most of the time, you'll probably find you want a class, rather
than a typedef. A class introduces a totally new type; it also
allows you to add validations to the operations. (Of course,
for simple things like WidgitCount, a class might be overkill.
Theoretically nicer, but a lot of extra code for a very small
benefit.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top