|
C++ lets you give a new name to a type using typedef. Declaration
typedef existingType newName;makes newName refer to existingType. For example,
typedef int* IntPtr;makes IntPtr mean int*. Then, declaration
IntPtr p;
creates variable p of type int*.
You typically make type definitions outside of functions,
often in header files.
A type definition does not call for a textual substitution. It treats the new type as atomic, or as a unit. Recall that declaration
int* p, q;
says that p has type int* and q has type int.
But
IntPtr p, q;
says that p and q both have type IntPtr, which is the
same as int*. As you can see, having names for pointer types
can make a program clearer.
Do not define a type that is already defined. Typically, that is relevant in modules, the topic of the next page. Do not define a type both in a header file stuff.h and in the associated implementation file stuff.cpp. The standards require that.
|