typedef

This is a discussion on typedef within the C++ Programming forums, part of the General Programming Boards category; typedef What this keyword exactly does? It's work is like pre-proccessor directives, but it does not have "#" prefix!!...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    typedef

    typedef
    What this keyword exactly does? It's work is like pre-proccessor directives, but it does not have "#" prefix!!

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    typedef defines a user-defined type. So:
    Code:
    typedef unsigned long long ULLong;
    defines a new type, ULLong, that can be used in variable and member declarations. typedef can be used to define both simple types and structured types, such as in
    Code:
    typedef struct tabent_s {
        UULong value;
        char name[200];
    } tabent_t;
    which defines a new type called tabent_t that can be used where you might otherwise use struct tabent_s. The namespace for structs and user-defined types is separate in C, so only one type is defined in the above example, but in C++, defining the named struct automatically defines a type of the same name, so in C++ the typedef in the above example defines two identical types, tabent_s and tabent_c.

    You can think of typedef as a short-hand, similar to #define, but it's not handled by the pre-processor, and the compiler can do strong type checking on user defined types, thus making the code more portable and robust.
    Last edited by filker0; 01-24-2006 at 07:27 AM.
    Insert obnoxious but pithy remark here

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Thanks
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    long long var?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Actually, typedefs aren't new types, they're just aliases. In other words:
    Code:
    typedef int myint;
    
    myint mi = 3;
    int i = mi;
    mi = i;
    There is absolutely no type conversion going on here, not even implicit. Typedefs could, in many cases, be replaced using search&find (or by the preprocessor):
    Code:
    void func(int i);
    void func(myint i); // Overloading error: signatures aren't different.
    See also 7.1.3 in the C++ standard.

    So why not just use the preprocessor? Because a typedef is aware of being a C++ construct. The most important case here are pointers. You probably know about the dangers of this syntax:
    Code:
    int* a, b;
    a is a pointer, but b is not! b is just a plain int.
    Now suppose you introduce a preprocessor macro pint as a pointer to int:
    Code:
    #define pint int*
    This works fine for a time:
    Code:
    pint a;
    pint b;
    But it will fail for this, because the preprocessor only does text substitution.
    Code:
    pint a, b;
    Again, (but far less obvious even for an experienced coder), a is a pointer but b is not. That's because the code after expansion looks again like this:
    Code:
    int* a, b;
    Typedefs are aware of C++ syntax. A typedef'd name is a unit and cannot be taken apart:
    Code:
    typedef int *pint;
    pint a, b;
    Both a and b are pointers here.
    A similar case:
    Code:
    #define mpint int*
    typedef int *tpint;
    
    const mpint a;
    const tpint b;
    What are the types of a and b?
    a is a pointer to const int. b is a const pointer to int.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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