Is there any difference between structure and union if we have only one member?

梦想的初衷 提交于 2019-12-06 16:32:41

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/26572432/is-there-any-difference-between-structure-and-union-if-we-have-only-one-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!