C++ 中的联合实际上是一个类吗?

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

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

问题描述

一位初级开发人员问我是否可以为带有 POD 参数的联合重载赋值运算符,以便在将联合实例分配给该类型的变量时写入联合中的相应数据类型.我回答说我不这么认为,但随后玩弄了以下代码.令我惊讶的是,这段代码实际上已编译(在 Ubuntu 12.04 上使用 g++ 版本 4.6.3)

union unMember{浮动 fData;无符号整数 uiData;unMember():uiData(0) {};unMember(浮动数据):fData(数据){};unMember(无符号整数数据):uiData(数据){};运算符浮动(){返回fData;};运算符 unsigned int() {return uiData;};非会员&运算符=(浮点数据){fData = 数据;返回 *this;};非会员&运算符=(无符号整数数据){uiData = 数据;返回 *this;};float GetFloat() const {return fData;};};诠释主要(){浮动 fTest = 1.0;无符号整数 uiTest = 10;非会员数据 = fTest;unMember data2 = uiTest;取消会员 data3 = data2;浮动 f = data.GetFloat();返回0;}

这让我意识到我对联合几乎一无所知(至少在 C++ 而不是 C 的上下文中),因为我真的没想到能够以这种方式为联合定义成员函数.上面的代码向我表明,在 C++ 中,联合是在内部作为类实现的,但实际上是 C++ 标准中的情况还是这只是 g++ 编译器的一些怪癖?

另外,因为这真的动摇了我对联合是什么的理解,我欢迎任何评论以这种方式为联合实现成员函数是否可取?在我的联合的上述实现中有什么本质上不安全的吗?

过去,我只在容器类中使用联合,该容器类有一个指示变量,用于存储联合中实际写入的类型,老实说,我认为这是它们的主要用途.以这种方式为联合重载构造函数等实际上很常见吗?

解决方案

C++ 中的联合实际上是一个类吗?

是的.在 C++ 中,联合是一个类:一种特殊的类.

C++11:9 类 (p5):

<块引用>

联合是用类键联合定义的类;它一次只保存一个数据成员


<块引用>

以这种方式为联合重载构造函数等实际上很常见吗?

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

9.5 联合(p2):

<块引用>

联合可以具有成员函数(包括构造函数和析构函数),但不能具有虚拟 (10.3) 函数.联合不应有基类.联合不得用作基类.

11 会员访问控制(p3):

<块引用>

使用关键字class定义的类的成员默认是private.使用关键字 structunion 定义的类的成员默认为 public.

因此,您可以像在类中那样重载构造函数、析构函数和运算符.

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;
}

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?

解决方案

Is a union in C++ actually a class?

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

C++11: 9 Classes (p5):

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:

9.5 Unions (p2):

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 Member access control (p3):

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++ 中的联合实际上是一个类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C++ 中的联合实际上是一个类吗?

基础教程推荐