Sign in to follow this  
AAAP

syntax question

Recommended Posts

SiCrane    11839
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
Roboguy    794
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
stylin    758
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
SiCrane    11839
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

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