Is a union in C++ actually a class?

試著忘記壹切 提交于 2019-11-29 11:48:17

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23932389/is-a-union-in-c-actually-a-class

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