Possible Duplicate:
Why compiler doesn't allow std::string inside union ?
I knew that I had this problem when I started with C++: The compiler wouldn’t allow me to put a variable of the type std::string into unions.
That was years ago, but actually I still don’t know the exact answer. I read something related to a copy function with the string that the union didn’t like, but that’s pretty much all.
- Why are C++ STL strings incompatible with unions?
From Wikipedia:
Think about it this way: If you have a union of a class type like
std::stringand a primitive type (let’s say along), how would the compiler know when you are using the class type (in which case the constructor/destructor will need to be called) and when you are using the simple type? That’s why full-fledged class types are not allowed as members of a union.