Typedef advantages?

This is a discussion on Typedef advantages? within the C++ Programming forums, part of the General Programming Boards category; Just wondering if anyone has solid advice on when and how to use typedef....

  1. #1
    LPP
    LPP is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    24

    Typedef advantages?

    Just wondering if anyone has solid advice on when and how to use typedef.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Use typedef to give a long and difficult type an alias.
    Code:
    typedef std::map< int, vector<string> > StrIntMap;
    typedef int(*compare)(void *a, void *b) CompFn;
    It is better to assume that there is no advantage at all, and only use typedef if writing out a variable's type is very fugly looking or especially error prone.
    Last edited by whiteflags; 10-19-2006 at 11:14 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> writing out a variable's type is very fugly looking or especially error prone.
    That is the advantage.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Use it when you gain a lot more in terms of clarity than you lose with the loss of detail in the code.

    Function pointers are notoriously hard to get right, and exceedingly verbose if you have several parameters. This is usually a good idea to typedef these.

    For a given T, using a typedef for T*
    Eg. typedef int *intp;
    You haven't saved any typing, you may as well type a * before something as type a 'p' after it. But you have made the code a hell of a lot harder to trace through since the number of *'s you use on the usage no longer agrees with the number of *'s on the declaration.
    This is a bad use of typedef.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    LPP
    LPP is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    24
    Excellent! Thank you... I had never really understood this. I already have several uses for this.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    If you write any class which you want to be usable like an STL container, you'll need to typedef things like iterators and the like.

    Also, citizen, I think your function pointer typedef should just be:

    Code:
    typedef int(*CompFn)(void *a, void *b);
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

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, 11:36 AM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06: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, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21