Possible Duplicate:
Difference between 'struct' and 'typedef struct' in C++?
Is there a difference between
typedef struct{
....
} mystruct;
and
struct mystruct{
....
};
?
Possible Duplicate:
Difference between 'struct' and 'typedef struct' in C++?
Is there a difference between
typedef struct{
....
} mystruct;
and
struct mystruct{
....
};
?
It's useless in C++. In C, it's because structs have their own namespace (you need to write struct T
if you don't typedef to something else).
In C, the syntax to declare a struct is struct mystruct var;
, so developers often typedef an anonymous struct to make declaring as simple as mystruct var;
. C++ allows you to define structs without the struct
keyword, so the typedef is used less often.
This idiom is commonly used in C, where a struct variable would need to be declared as struct StructName myStruct
, and StructName myStruct
wouldn't work. It's not necessary in C++.