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

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

In C++, is there any difference between:

struct Foo { ... };

and:

typedef struct { ... } Foo;
8条回答
  •  北海茫月
    2020-11-21 06:28

    An important difference between a 'typedef struct' and a 'struct' in C++ is that inline member initialisation in 'typedef structs' will not work.

    // the 'x' in this struct will NOT be initialised to zero
    typedef struct { int x = 0; } Foo;
    
    // the 'x' in this struct WILL be initialised to zero
    struct Foo { int x = 0; };
    

提交回复
热议问题