开发者

How union is used to define a class

I have two doubts, please help开发者_StackOverflow社区 me on this:

  1. Is it possible to define a class inside union
  2. Is it possible to define a class without class name


1 - yes with restriction that class has no constructor or destructor 2 - yes

Following code aggregates both as an example:

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

};

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


Is it possible to define a class inside union

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

Is it possible to define a class without class name

Yes, it is possible to create anonymous classes, as in:

class
{
     // ... body of class ...
} name_of_instance;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜