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

原址

摘抄:

In C++, there is only a subtle difference. It's a holdover from C, in which it makes a difference.

1. In C, there are two different namespaces of types: a namespace of struct/union/enum tag names and a namespace of typedef names. If you just said:

struct Foo { ... };
Foo x;

You would get a compiler error, because Foo is only defined in the tag namespace. You'd have to declare it as:

struct Foo x;

2. Any time you want to refer to a Foo, you'd always have to call it a struct Foo. This gets annoying fast, so you can add a typedef:

struct Foo { ... };
typedef struct Foo Foo;

Now both struct Foo (in the tag namespace) and just plain Foo (in the typedef namespace) both refer to the same thing, 

3. and you can freely declare objects of type Foo without the struct keyword. The construct

typedef struct Foo { ... } Foo;

is just an abbreviation for the declaration and typedef

4. Finally,

typedef struct { ... } Foo;

declares an anonymous structure and creates a typedef for it. Thus, with this construct, it doesn't have a name in the tag namespace, only a name in the typedef namespace. This means it also can't be forward-declared. If you want to make a forward declaration, you have to give it a name in the tag namespace.(注:匿名的结构体定义也无法自定义各种构造函数)

5. In C++, all struct/union/enum/class declarations act like they are implicitly typedef'ed, as long as the name is not hidden by another declaration with the same name. See Michael Burr's answer for the full details.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值