• Advertisement
Sign in to follow this  

what is the typdef statement mean?

This topic is 3538 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

For example: typedef int Map[MapSizeX][MapSizeY]; what does that mean? Where can I learn about typedef? Is it like a class?

Share this post


Link to post
Share on other sites
Advertisement
That example typedef is broken. :-)

It lets you create another name that refers to the same type as an existing one.

e.g. typedef unsigned uint;

Means that I can now do

uint i;

as opposed to

unsigned i; (equivalent to unsigned int i;)

Note: it introduces a synonym - it doesn't replace the existing name (so both work). Standard scoping rules apply.

It's primarily useful if you're using rather lengthy type names, for the sake of making reading easier (particularly if you're working with iterators of templated types, such as std::vector<std::vector<std::pair<unsigned, std::string> > >::const_iterator - although that is a somewhat contrived example).

For more information, simply plugging 'typedef' into Google will yield plenty.

[Edited by - TheUnbeliever on May 17, 2008 11:07:28 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by TheUnbeliever
That example typedef is broken. :-)

Hm, it seems fine to me. Map is introduced as a new name for int[MapSizeX][MapSizeY]. The syntax may seem strange at first glance, but I don't see any errors.

Share this post


Link to post
Share on other sites
In my mind I just see typedef as being like a shortcut.

Example:
typedef unsigned short int ushort;

That code, if placed before the start of your main function will create a shortcut, so when you type
ushort cityPopulation

then it will make cityPopulation an unsigned short.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by TheUnbeliever
That example typedef is broken. :-)

Hm, it seems fine to me. Map is introduced as a new name for int[MapSizeX][MapSizeY]. The syntax may seem strange at first glance, but I don't see any errors.


Does it not need to be

typedef int[MapSizeX][MapSizeY] Map;

rather than

typedef int Map[MapSizeX][MapSizeY];

though?

EDIT: Apparently not (the first spews 8 errors, the second none). You learn something every day, I guess!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement