How union is used to define a class

I have two doubts, please help me with this:

  • Is it possible to define a class inside union
  • Is it possible to define a class without a class name
+3
source share
2 answers

1 - yes with the restriction that the class does not have a constructor or destructor 2 - yes

The following code aggregates are an example:

union MyUnion
{
    class 
    {
        public:
        int a;
        int b;
    } anonym_access;
    double align;

};

int main()
{
    MyUnion u; //instance checks if it is compileable
}
+5
source

Is it possible to define a class inside a union

A union can contain any simple data type (POD) type. Types with a non-trivial constructor or destructor are not PODs and therefore cannot be used in a union. For these types, you can use boost :: variant .

, , :

class
{
     // ... body of class ...
} name_of_instance;
+4

Source: https://habr.com/ru/post/1744668/


All Articles