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