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

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

In C++, is there any difference between:

struct Foo { ... };

and:

typedef struct { ... } Foo;
8条回答
  •  后悔当初
    2020-11-21 06:34

    You can't use forward declaration with the typedef struct.

    The struct itself is an anonymous type, so you don't have an actual name to forward declare.

    typedef struct{
        int one;
        int two;
    }myStruct;
    

    A forward declaration like this wont work:

    struct myStruct; //forward declaration fails
    
    void blah(myStruct* pStruct);
    
    //error C2371: 'myStruct' : redefinition; different basic types
    

提交回复
热议问题