是C ++中的联合实际上是一个类吗? [英] Is a union in C++ actually a class?

查看:104
本文介绍了是C ++中的联合实际上是一个类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个初级开发者问我是否可能重载一个具有POD参数的联合的赋值运算符,使得当联合的实例被分配给该类型的变量时,联合中的相应数据类型将被写入。我回答说,我没有这么认为,但随后玩弄以下代码。令我吃惊的是,这段代码实际上是编译过的(在Ubuntu 12.04上使用g ++版本4.6.3)

A junior developer asked me if it was possible to overload assignment operators for a union with POD arguments such that the corresponding data type within the union would get written to when an instance of the union is assigned to variable of that type. I replied that I did not think so, but then played around with the following code. To my surprise this code actually compiled (using g++ version 4.6.3 on Ubuntu 12.04)

union unMember 
{

  float fData;
  unsigned int uiData;
  unMember():uiData(0) {};
  unMember(float data):fData(data) {};
  unMember(unsigned int data):uiData(data) {};

  operator float() {return fData;};
  operator unsigned int() {return uiData;};

  unMember& operator=(float data) {fData = data;return *this;};
  unMember& operator=(unsigned int data) {uiData = data; return *this;};

  float GetFloat() const {return fData;};
};

int main () {

  float fTest = 1.0;
  unsigned int uiTest = 10;
  unMember data = fTest;

  unMember data2 = uiTest;
  unMember data3 = data2;

  float f = data.GetFloat();

  return 0;
}



这使我意识到我根本不知道工会至少在C ++而不是C的上下文中),因为我真的没有想到能够以这种方式为联合定义成员函数。上面的代码告诉我,在C ++中,一个联合在内部实现为一个类,但实际上是C ++标准中的情况,还是这只是g ++编译器的一些奇怪?

This has made me realise that I know pretty much nothing at all about unions (at least in the context of C++ rather than C) as I really did not expect to be able to define member functions for a union in this way. The above code suggests to me that in C++ a union is implemented internally as a class, but is that in fact the case in the C++ standard or is this just some quirk of the g++ compiler?

此外,由于这真的动摇了我对于工会的理解,我欢迎任何关于是否建议以这种方式实现工会的成员职能的意见?在我的联合的上述实现中有什么固有的不安全吗?

Also, because this has really shaken my understanding of what a union is, I would welcome any comment on whether it is advisable to implement member functions for unions in this way? Is there anything inherently unsafe in the above implementation of my union?

在过去,我只使用一个容器类中的联合,它有一个指示符变量,实际上写在联盟和老实说,我认为这是他们的主要用途。实际上通常以这种方式重载构造函数等等。

In the past I have only used unions within a container class that has an indicator variable that stores what type has actually been written to in the union and to be honest I thought that was their primary use. Is it actually common to overload constructors etc for unions in this way?

推荐答案


C ++实际上是一个类?

Is a union in C++ actually a class?

是的。在C ++中,union是一个类:一种特殊类型的类

Yes. In C++ a union is a class: a special kind of class.


是用类 - 键联合定义的类;一次只能有一个数据成员

A union is a class defined with the class-key union; it holds only one data member at a time








这是真的常见的重载构造函数等工会的这种方式吗?

Is it actually common to overload constructors etc for unions in this way?

联盟是一个特殊的类,有一些限制:

A union is a special class with some restrictions:


联合可以有成员函数(包括构造函数和析构函数), 10.3)函数。联合不应有基类。联合不应用作基类。

A union can have member functions (including constructors and destructors), but not virtual (10.3) functions. A union shall not have base classes. A union shall not be used as a base class.



11会员访问控制(p3):



11 Member access control (p3):


默认情况下,使用关键字 class 定义的类的成员为 private 。使用关键字 struct union 定义的类的成员为 public

Members of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or unionare public by default.

所以,你可以重载构造函数,析构函数和操作符,就像你在类中一样。

So, you can overload constructors, destructors and operators similar to as you can do in a class.

这篇关于是C ++中的联合实际上是一个类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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