I would like to know the difference between structure and union for one member data type if there is any.
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
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.