Why doesn't this work:

#include <iostream>


class myclass {
    public:
union d {

    int i;
};
};

int main() {
myclass i;
i.d.i = 3;

return 0;
}

but this does:

#include <iostream>


class myclass {
    public:
union {

    int a;
};
};

int main() {
myclass i;
i.a = 3;

return 0;
}

Union with a name seems a lot like a structure or a class, but a union without a name seems pointless to me. So whats the point of it? The first example just looks like a declaration of data type inside a declaration of another data type, and that supposed to be impossible, but the compiler only gives me an error when I try to reference there variables.... So can someone explain this to me? :D

How about

union {
    int i;
} d;

?

A union of one, hmmm...

>>Union with a name seems a lot like a structure or a class,

That's because a union is a heck of lot like a struct or class except that all members share the same address in memory and the union is as large as the largest (aligned) data member it contains. Other than that, unions are similar to structs and classes in many ways, they can even be templated, have member functions, etc. But they have several restrictions, like they cannot have virtual members, cannot contain non-POD data members, cannot be part of an inheritance scheme, etc. According to the standard (C++98), unions are considered to be a kind of "class". And the new standard will even expand the capabilities of a union-types.

>>but a union without a name seems pointless to me.

It is certainly not pointless. Say you want a class that has some data members to occupy the same memory and others to have separate places in memory, then you can use a nameless union for that.

>>So whats the point of it?

The point is to simplify the syntax such that you don't have to name the union data member each time you want to address it.

>>The first example just looks like a declaration of data type inside a declaration of another data type

That's exactly what it is.

>>and that supposed to be impossible,

Who told you that? Declaring a type within a type is very common. It is called nested types. You can have nested everythings: nested typedefs, nested classes, nested structs, and nested union types. This is incredibly useful and common. In fact, the entire world of template meta-programming is based on this feature and others.

>>but the compiler only gives me an error when I try to reference there variables

The error the compiler gives you is the same error that you would get if you did:

struct A {
  int a;
};

int main() {
  A.a = 42;  //Error: left of "." must be an object of a class-type.
// or even,
  A::a = 42;  //Error: "a" is not a static member of class A.
};

To access the nested union-type "d" you need to write myclass::d and to use the variable "i" in that union, you have to create an object of that union-type.

If you want to use the syntax that you wrote (i.d.i), you can do either this:

class myclass {
  public:
    union {
      int i;
    } d;      //the name of a data member must follow the type declaration.
};

Or:

class myclass {
  public:
    union d_type { //the name of the union-type goes here.
      int i;
    } d;      //the name of a data member must follow the type declaration.
};

Or:

class myclass {
  public:
    union d_type { //the name of the union-type goes here.
      int i;
    };      
    d_type d; //declare the data member.
};
commented: that explains a lot! Thanks! +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.