如果我们只有一个成员,结构和联合之间有什么区别吗? [英] Is there any difference between structure and union if we have only one member?

查看:101
本文介绍了如果我们只有一个成员,结构和联合之间有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一种成员数据类型在结构和联合之间的区别(如果有的话)。

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

推荐答案

在C:没有。著名的节省空间的笑话 #define struct union 几乎不是笑话。

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

在C ++中98:联盟只能具有 POD成员,非联盟类可以具有任意成员。

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

在C ++ 11中:联合可以具有对象类型(但不是引用类型)的任意数据成员,但与非联合类相比,它们的使用受到更多限制。 (即:联合不能具有虚拟成员函数,不能是基类并且不能具有基类。)而且,与单成员非联合类相比,您必须编写更多代码以使单成员联合工作。 ,因为您必须自己编写构造函数和析构函数。

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.

这篇关于如果我们只有一个成员,结构和联合之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆