Sign in to follow this  
meatcow

C++ defines vs typedefs

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this