Jump to content
  • Advertisement
Sign in to follow this  
Dragonion

A difference between typedef and #define?

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

Hi

Can anyone come up with an example where you can not use this
[source]typedef int MyIntType;[/source]instead of this
[source]#define MyIntType int[/source]or vice versa for primitive data types?

Personally I would prefer a typedef since that would enable the compiler to report the correct type in case of an error, but I can actually not think of a situation where one style would have limitations compared to the other (unlike using #defines for constant values instead of declaring a const variable, for example).

EDIT: Oh yes, and besides the fact that typedefs are bound by namespaces, of course.

Share this post


Link to post
Share on other sites
Advertisement
As usual, #define doesn't respect syntax, and multiple tokens pasted by the preprocessor can associate in unexpected order.

[source]
typedef char * mystring_t;
#define mystring_d char *

int a,b;
mystring_t c,d;
mystring_d e,f;
[/source]
a and b are or type int.
c and d are of type char *.
e is of type char *, but f is not.

Share this post


Link to post
Share on other sites
Using a define for that is never acceptable imho.

I agree. It's ugly.

As usual, #define doesn't respect syntax, and multiple tokens pasted by the preprocessor can associate in unexpected order.

[source]
typedef char * mystring_t;
#define mystring_d char *

int a,b;
mystring_t c,d;
mystring_d e,f;
[/source]
a and b are or type int.
c and d are of type char *.
e is of type char *, but f is not.

Ah, very good point! This definitely shows that there is in fact a big difference. Thanks.

Share this post


Link to post
Share on other sites
If you are using an external library with an identifier containing "MyIntType", you could get strange and inconsistent results.

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!