Jump to content
  • Advertisement
Sign in to follow this  
meatcow

C++ defines vs typedefs

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

I'm struggling to define a clear difference between defines and typedefs in C++, and when each should be used. So far the only difference i can tell is that defines work in a pre-processor step. Can someone please clarify the differences? thanks.

Share this post


Link to post
Share on other sites
Advertisement
Yep, #define is a preprocessor directive that defines a token which the preprocessor substitutes with whatever the token has been defined as when the token is put into code. Preprocessing is the first step of the compilation process. The result from preprocessing is the actual code that gets compiled.

A typedef defines a custom data type, typically a structure grouping together other data types.

That's it in a nutshell. I'm certain someone else will provide a more pedagogically accurate definition for you.

Share this post


Link to post
Share on other sites

#define NUMBAR 82 //legal
typedef 82 numbar; //illegal


defines can do damn near anything. All typedefs do is create an alias for a type. As a general rule, if you can choose between the two, choose the typedef. Similar rules exist for most uses of #define in C++.

CM

Share this post


Link to post
Share on other sites
The difference isn't too great, but it's better to use a typedef. A define is just a literal replacement (and you're right, in the preprocessor).

See http://syque.com/cstyle/ch9.3.htm section 9.3.3

Share this post


Link to post
Share on other sites
The biggest thing between #define and typedef is how they behave in relation to scope. All pre-proc macros (including #define) ignore scope while typedefs will obey it.


class SomeClassA
{
typedef unsigned int SomeID;
}

class SomeClassB
{
typedef unsigned short SomeID;

};


No problems there but take to make that work with #defines, you would have to make the two names unique somehow which can distract from the flow in some places.

The biggest that I find myself using typedefs for is inside classes. Mostly to make things easier to read. For example


class A
{
typedef std::map< unsigned int, std::string > HashMap;

public:
const std::string& GetString( unsigned int id )
{
HashMap::iterator itr = m_hashMap.find( id );

if( itr == m_hashMap.end() )
//this is bad no string found

return itr.second
}

HashMap m_hashMap;
}


Doing this with macros would cause problems if you had different types of maps in different classes that did different things.

Edit: too late for me to be typin heh

[Edited by - Sutekh on April 3, 2006 1:30:04 AM]

Share this post


Link to post
Share on other sites
since no-ones mentioned it, i will. you can use typedef's to make life easier in regards to function pointers in C/C++. typedef's are also alot more syntax error friendly when being compiled (because they arn't just a blind replace like pp-macros), compiler errors from typedef's are usually actually understandable and usefull and definately more often than not less obfuscated than pp-macro caused syn errors.

Cheers
-Danu

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!