Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of union inside a class

Tags:

c++

class

unions

I saw some code as follows:

class A 
{
private:
    union {
        B *rep;
        A *next;
    }; // no variables of this anonymous defined!

    void func()
    {
        A *p = new A;

        p->next = NULL; // why p has a member variable of 'next'?
    }
};

I have compiled the above code with VS2010 without any error. Here is the question,

why p has member variable 'next'?

    union {
        B *rep;
        A *next;
    };

As far as I know, this is an anonymous union without even defining a variable. How can we access the member variables inside this union like that?

like image 870
q0987 Avatar asked Mar 27 '11 23:03

q0987


People also ask

Can we use union inside structure?

A structure can be nested inside a union and it is called union of structures. It is possible to create a union inside a structure.

Why do we use union inside structures in C?

A union is an object similar to a structure except that all of its members start at the same location in memory. A union variable can represent the value of only one of its members at a time. In C++, structures and unions are the same as classes except that their members and inheritance are public by default.

What is the use of union in C++?

A union is a user-defined type in which all members share the same memory location. This definition means that at any given time, a union can contain no more than one object from its list of members.

What is a class union?

A union is a special class type that can hold only one of its non-static data members at a time. The class specifier for a union declaration is similar to class or struct declaration: union attr class-head-name { member-specification } attr.


2 Answers

Because that's pretty much what an anonymous union does, it defines zero-or-more variables in the enclosing namespace (which in a class declaration makes them field names) which occupy overlapping memory. Hence in use it's the same as if you'd declared

class A 
{
private:
    B *rep;
    A *next;

    void func()
    {
        A *p = new A;

        p->next = NULL;
    }
};

...except for rep and next occupying overlapping space (or given that the two pointers will have the same size, the same space), and hence all the dangers and benefits that come with a named union.

like image 81
Jon Hanna Avatar answered Sep 29 '22 03:09

Jon Hanna


Here's the quote from the standard that controls this behavior: section [class.union] (wording from C++0x draft n3242)

A union of the form union { member-specification } ; is called an anonymous union; it defines an unnamed object of unnamed type. The member-specification of an anonymous union shall only define non-static data members. [ Note: Nested types and functions cannot be declared within an anonymous union. — end note ] The names of the members of an anonymous union shall be distinct from the names of any other entity in the scope in which the anonymous union is declared. For the purpose of name lookup, after the anonymous union definition, the members of the anonymous union are considered to have been defined in the scope in which the anonymous union is declared.

like image 31
Ben Voigt Avatar answered Sep 29 '22 02:09

Ben Voigt