Is it possible to put several objects together inside a union?

后端 未结 6 1943
无人及你
无人及你 2021-02-08 06:38

What if I have this:

union{
    vector intVec ;
    vector floatVec ;
    vector doubleVec ;
} ;

Of cours

6条回答
  •  北恋
    北恋 (楼主)
    2021-02-08 06:50

    You cannot have unions containing non-POD class types. Your sample will not compile.

    You can use boost::variant as a safe alternative to C unions. See the documentation on boost.org. You might, however, reconsider your design and use polymorphism instead. Depends on what you're trying to accomplish, of course.

提交回复
热议问题