Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.



This topic is 5744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post

Link to post
Share on other sites
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?

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Typedef allows aliasing for types. There are two main uses for it. First, sometimes there is a complex type declaration such as:

int*** myVar[10]

Which is an array of triple pointers to integers. This is relatively unwieldy, so you could make a type alias. Type aliasing is even more useful if you decide to take up the necromantic (i.e. function pointer) arts.

typedef int*** MyTable[10];

allows one to just say

MyTable myvar;

The other benefit is the same as using constants rather than magic numbers. Say you were creating a tile-based game. Initially you might think that no level would be larger than 256x256, and use chars as indexes. But if you did some kind of typedef

typedef char ArrayIndex;

and later decided that you wanted larger levels, you could easily change all of your index types by just changing the single typedef statement.


Share this post

Link to post
Share on other sites
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 >
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;

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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();

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();

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.


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

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

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

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

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!