Jump to content
  • Advertisement
Sign in to follow this  
AAAP

syntax question

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

Is there a difference between "typedef struct/class" and just "struct/class"? Or is it just a style thing.

Share this post


Link to post
Share on other sites
Advertisement
In C there are two namespaces: the normal namespace and the struct namespace. In C a declaration like:

struct StructName;

places StructName in the struct namespace. However

typedef struct StructName AnotherName;

puts the AnotherName type alias in the global namespace. In C you would use the typedef struct thing so that you can refer to it without typing struct in front of the name of the type everytime you refer to it. In C++ that kind of thing is pretty much pointless.

Share this post


Link to post
Share on other sites
In C++, they are exactly the same. I wouldn't call it a style thing, since it doesn't change anything or really make anything more explict/implicit.

Share this post


Link to post
Share on other sites
typedef struct someStruct { ... } someStruct_t is used in C as a shortcut since the struct keyword is required when declaring structs. It's not generally used in C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
In C++, they are exactly the same. I wouldn't call it a style thing, since it doesn't change anything or really make anything more explict/implicit.


They aren't exactly the same. This is a struct named S:

struct S {
// data
};

This is an anonymous struct with a type alias named S:

typedef struct {
// data
} S;

There are a number of differences between the two (most of which only matter if you're writing the compiler), but the big difference is that depending on the compiler, the second form can give you much more confusing error messages. Basically there's no reason to use the second form in C++.

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.

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!