Is there any difference between structure and unio

2020-06-30 04:16发布

I would like to know the difference between structure and union for one member data type if there is any.

1条回答
劫难
2楼-- · 2020-06-30 04:45

In C: None. The famous "space-saving joke" #define struct union is almost not a joke.

In C++98: Unions can only have POD members, non-union classes can have arbitrary members.

In C++11: Unions can have arbitrary data members of object type (but not of reference type), but their use is more restricted that that of non-union classes. (Namely: a union cannot have virtual member functions, cannot be a base class and cannot have base classes.) Also, you have to write more code to make a one-member union work as opposed to a one-member non-union class, since you have to write constructors and the destructor yourself.

查看更多
登录 后发表回答