Thread: typedef...

  1. #1
    Registered User
    Join Date
    Apr 2004


    what exactly is "typedef"?

  2. #2
    Registered User
    Join Date
    Nov 2001
    typedef define a type alias. It is a good solution for defining an otherwise long expression.

    // typedef used for defining a function pointer.
    typedef TypeBase (*TypeDefExample)(int x, char y, TypeClient z);


  3. #3
    Hardware Engineer
    Join Date
    Sep 2001

    It can reduce typing, typing errors, and describe the purpose of the variable

    You'll often see this:

    typedef unsigned long ULONG;
    unsigned long X = 0;     // This is the 'normal' way
    ULONG Y = 0;                // This requires typedef
    Personally, I (generally) avoid typedef because it makes code harder to read. There are only a few actual types, and I don't like having to go find the typedef to find-out what the actual type is.

    I suppose it's OK when it's something more complicated like Kuphryn's pointer example. The <windows> header has lots (maybe hundreds) of typedefs. This means that there are several "different" Windows-defined "types" that are really the same types (pointers to long integers, etc.) The advantage of the Windows typedefs, is that the type provides some context, some hint about the purpose of the variable. The disadvantage is that you have to check the WinAPI documentation to find-out what the real type is.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Typedefs are useful for the following reasons.

    1. To help with portability.
    Very few types have the same size across all machines, and the standard only specifies a minimum size for things like char, short, int and long.
    So you might say
    typedef unsigned short UINT16;
    to give you some idea of the range of a data type. Should you ever need to change it, then you only have one typedef to change instead of lots of 'unsigned short'.

    2. To document meaning in your code
    If you were working with lots of distance measurements say, then you could do
    typedef double distance_t;
    distance_t here, there, everywhere;
    3. Complex declarations
    Function pointers (as shown by kuphryn) get very hairy very quicky. A typedef saves an awful lot of complex verbage in the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM