Skip to main content
GameDev.net gamedev.net
🔒 Locked

Typedef

Started by TheShadow Mar 23, 2003 at 5:05 PM 5 replies 300+ views
Original Post
TheShadow
TheShadow
I am reading a c++ tutorial and it uses the typedef word... I dont get what it is for or why to use it.
Mecha Engineer (Making Real Humanoid Suits)
Neosmyle
Neosmyle
Basically just laziness and to save time.

All it does is create a "nickname" for a variable type, etc.

With that in mind you should be able to understand it from any c/c++ documentation.
TheShadow
TheShadow
so if i have


  
struct house {
char nameofpropietary;
};

typedef struct house TheMillersHouse;
typedef TheMillersHouse *MillersHouse;




What is that pointer useful for? And why use typedef?
Mecha Engineer (Making Real Humanoid Suits)
JohnBolton
JohnBolton
I primarily use typedef for two reasons:

1. As an alias for long or complicated declarations.
2. An alias for template parameters.


  
template< typename C, typename T >
class izfstream : public std::basic_istream< C, T >
{
public:
typedef C char_type;
typedef T traits_type;
typedef izfstream this_type;
typedef std::basic_istream< C, T > base_type;
typedef std::basic_ios< C, T > ios_type;
...
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Shannon Barber
Shannon Barber
quote:
Original post by TheShadow
I am reading a c++ tutorial and it uses the typedef word...

I dont get what it is for or why to use it.


It let''s you apply a type re-direction in your code. Instead of using "int" as the error code, you can say
typedef int errorcode;
Then make functions that return an errorcode. Later you can replace the typedef with anohter typedef, or a class or structure, and change how it behaves.

With C code, you could write some algorithm and make a verion for both floats and doubles by changing a typedef. With C++ you would probably want to use a template, and then you use typedef''s so that the template class can remember it''s types.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Qw3r7yU10p!
Qw3r7yU10p!
It's also useful for reducing coding and making code more readable and easier to change.

For example using the standard containers in the C++ library you can end up with long declarations:


  
std::map<Player*,IPAddress*> playerIPs;
std::map<Player*,IPAddress*>::iterator it = playerIPs.begin();
std::map<Player*,IPAddress*>::iterator itEnd = playerIPs.end();
//etc.




with a few typedefs you can make things a lot nicer


  
typedef std::map<Player*,IPAddress*> PlayerIPMap;
typedef PlayerIPMap::iterator PlayerIPMapIt;

PlayerIPMap playerIPs;
PlayerIPMapIt it= playerIPs.begin();
PlayerIPMapIt itEnd= playerIPs.end();
//etc.


The nice thing though is that if you have some class which you'd rather use than the standard map, one you've written yourself perhaps that will work with all the nice standard algorithms available in the standard library, you can just change the typedef and see how your program's performance changes.

eg

  
//typedef std::map<Player*,IPAddress*> PlayerIPMap;

typedef shadow::MyMap<Player*,IPAddress*> PlayerIPMap;
typedef PlayerIPMap::iterator PlayerIPMapIt;
//etc





[edited by - petewood on March 24, 2003 3:36:47 AM]

[edited by - petewood on March 24, 2003 3:37:48 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.