Thread: ? about typedef

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    ? about typedef

    Hi ,

    I am confused about what typedef means ?

    for example:

    "typedef double something; "

    what exactly does this mean??

    Thanks for any explanation!!

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    To define your own type based on other existing data types.
    the syntax:
    Code:
    typedef   existing_type   new_type_name ;
    where existing_type is a C++ fundamental or any other defined type and new_type_name is the name that the new type we are going to define will receive. For example:

    typedef char C;
    typedef short unsigned int USHORT;

    then just use the new type word to declare variables

    C 'a';
    USHORT 5;

    Typedef enforces strong typing, in accordance with general C++, therefore use this over Macro's..

    --Placid

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    it's the method for creating a type based on some other type in C/C++. Many people, not including myself will use it to create a name for a pointer to something. for instance:

    typedef int *intptr;


    then you can use it to make a pointer to an int later in your code.

    intptr ptr1;
    int *ptr2;

    ptr1 and ptr2 are essentially the same thing. Get it?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    beat me to it.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    If it helps, I think you explained it better

    --Placid

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    oh thanks a lot. shheeeeesshhh
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    For an example of how to abuse typedef consult the windows.h header file.


  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    it's also useful to substitute a shorter name for a type (which is always useful in avoiding typos).

    ie:
    typedef typename std::vector<Wrapper_of_type<T> >::iterator iter;

    for (iter i=begin(); i != end(); ++i) ...

    any change in the long name could be made at one place instead of many.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Yeah, doing something like:

    std::vector<std::vector<float> > f(rowcount, std::vector<float>(colcount));

    is not as nice as:

    typedef std::vector<float> row;
    typedef std::vector<row> fArray;

    fArray f(rowcount, row(colcount));

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