Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a union in C++ actually a class?

Tags:

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?

like image 603
mathematician1975 Avatar asked May 29 '14 11:05

mathematician1975


People also ask

Is union a class?

A union is a special class type that can hold only one of its non-static data members at a time. list of access specifiers, member object and member function declarations and definitions. A union can have member functions (including constructors and destructors), but not virtual functions.

What is the point of unions in C?

C unions allow data members which are mutually exclusive to share the same memory. This is quite important when memory is valuable, such as in embedded systems. Unions are mostly used in embedded programming where direct access to the memory is needed.

What are the limitations of union in C?

Disadvantages of Structure and Union in C In a union you can use only one union member at a time. All the union variables cannot be initialized or used with varying values at a time. Union assigns one common storage space for all its members. Structure is slower because it requires storage space for all the data.

What is the difference between struct and union in C?

Both structure and union are user-defined data types in C programming that hold multiple members of different data types. Structures are used when we need to store distinct values for all the members in a unique memory location while unions help manage memory efficiently.


1 Answers

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.

like image 124
haccks Avatar answered Oct 18 '22 16:10

haccks