Difference between 'struct' and 'typedef struct' in C++?

前端 未结 8 1642
忘了有多久
忘了有多久 2020-11-21 06:12

In C++, is there any difference between:

struct Foo { ... };

and:

typedef struct { ... } Foo;
8条回答
  •  粉色の甜心
    2020-11-21 06:27

    One more important difference: typedefs cannot be forward declared. So for the typedef option you must #include the file containing the typedef, meaning everything that #includes your .h also includes that file whether it directly needs it or not, and so on. It can definitely impact your build times on larger projects.

    Without the typedef, in some cases you can just add a forward declaration of struct Foo; at the top of your .h file, and only #include the struct definition in your .cpp file.

提交回复
热议问题