What does 'typedef' do?

This is a discussion on What does 'typedef' do? within the C++ Programming forums, part of the General Programming Boards category; What does the keyword 'typedef' do and what are the adavantages of using it in object oriented design? Ex: What ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    What does 'typedef' do?

    What does the keyword 'typedef' do and what are the adavantages of using it in object oriented design?
    Ex:
    What does
    Code:
    .
    .
    .
    public:
    typedef B bucket_type;
    typedef K key_type;
    mean? Thanks all.

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    There are no advantages in OOP for using typedef

    Here is an example of why you would used typedef with a struct.

    Code:
    struct car {
         char make[40];
         int year;
    } my_car, mycar2;
    
    struct car whatever;
    
    
    typedef struct { 
         char make[40];
         int year;
    } my_car;
    
    my_car VW;
    my_car your_car;
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    But I still don't know what typedef means? Could somebody clarify it a bit further? Thanks.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    It creates a type alias. For example
    typedef int int_32; makes int_32 an alias to int.
    It doesn't create a new type.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Nick, is correct. It is just an alias, it does NOT create a new data type.
    Mr. C: Author and Instructor

  6. #6
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    would this function any differently ( from the programmers point of view ) than using a preprocessor definiton,

    like would

    typedef int int_32;

    sort of work the same as

    #define int_32 int


    Just wondering because I have never used typedef before

  7. #7
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Yes, they are similar in function.
    Mr. C: Author and Instructor

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by MethodMan
    There are no advantages in OOP for using typedef
    Not true at all! There are still many uses of typedef in OOP.

    For one, simplification of names when dealing with a long datatypes, especially with templating. Also, for simplifying things such as the datatype:

    constant pointer to a volatile cost pointer to class "DMSegment" const member function with return type ErrorEnum that takes parameters const char* and AudioFlags called "Accessor".

    ErrorEnum ( DMSegment::* const volatile * const Accessor )( const char*, AudioFlags ) const

    Would you want to type that everytime you made a declaration!? If you were to be using the datatype multiple times, you'd probably want to typedef it to be able to use one word rather than that huge amount of characters. This example is a bit exagerated, but I think it's a pretty good illustration of my point. Just because it's not necissary to use when declaring a struct for name simplification, it can be used in other places for the same exact reason.

    Also, it's still extremely useful in making programs easily portable, especially concerning file-io:

    Say you have an application where the number of bytes an integer takes up actually does matter. You can't be guaranteed that all compilers have a short int that's 2-bytes, or an integer as 4-byte, or a long as 4-bytes, or a compiler-defined int as 64-bytes, or or even a char as signed or unsigned.

    In order to make it easy to port, you can typedef datatypes such as int32 or int64 to the associated types for that particular compiler. Then, when you go to compile it in another compiler, all you have to do is change that one file with the typedefs and you immediately are able to adjust any datatypes that must be of a certain size.

    One example of why it's necissary to use datatypes of particular length -- let's say you want to read in 4 bytes from a file and store it to an integer. If the integer isn't 4 bytes, then you'll either not write to the rest of the integer (if it's over 4 bytes), or worse, you'll write past the boundaries of the integer (if it's under 4 bytes).

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by beege31337
    would this function any differently ( from the programmers point of view ) than using a preprocessor definiton,

    like would

    typedef int int_32;

    sort of work the same as

    #define int_32 int


    Just wondering because I have never used typedef before
    No -- typedef obeys scoping rules while #define does not.

    You're better off using typedef in almost all cases

  10. #10
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    [Yes, they are similar in function.
    They are different, but I have seen them used in similar situations.

    You are better off using typedef- like was mentioned, especially with linked lists.


    __________________
    Mr. C: Author and Instructor

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, 09: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

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