Difference between 'struct' and 'typedef struct' in C++?
c-programmingcpptypedefstructnamespaces
Abstraction: C and C++ struct versus typedef struct namespace differences
Key points:
- In C, struct names live in a separate tag namespace; you must write
struct Foo xunless you typedef it, but in C++ struct/class/union names act as implicit typedefs. typedef struct { ... } Foocreates an anonymous struct with only a typedef-namespace name and cannot be forward-declared;typedef struct Foo { ... } Foogives both a tag and an alias.- Anonymous typedef structs do not support inline member initializers in some compilers; named structs do.
- In C++, a function or object with the same name and scope hides the tag name; adding a typedef makes such shadowing a compile-time error rather than a silent bug.
- Linux kernel coding style explicitly avoids typedef-ing structs to keep type identity visible at the call site.
- Forward declarations require a tag name; typedef-only structs cannot be forward-declared, increasing header coupling.
Connections: Stackoverflow · C Type System · Typedef
Source: http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c