Is there anything wrong with a union having one or more methods? Or anything to watch out for? (I can see constructors/destructors being problematic for schizophrenic reasons)
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
From the C++03 & C++0x (Draft N3092) standards:
Initializing the union using the aggregate initializer syntax (
U u = { 42 };
) or setting a member afterwards (U u; u.i = 42;
) is not "problematic". And neither is initializing it using a constructor (U u( 42 );
).The only "catch" is that you cannot use the aggregate initializer syntax for a union that has a user defined constructor.
How could you possibly implement such a thing? Here's a pointer to a union, hope you don't mind that you have no idea which variables are safe to use and which aren't.
Unions are a dead language feature really anyway- they've been totally superseded by library-based methods like boost::variant or boost::any. Kind of similar to the void* and functional macros - they're very rarely useful in C++ compared to other options.