问题
can somebody please explain why the following program causing the compilation problem. I have compiled the source code over VS2013.
#include <iostream>
using namespace std;
// Do not work
union myuni
{
string str;
};
void main()
{
}
Does union require the fixed length size while declaring it? The same scenario works fine with structure.
回答1:
You cannot have a string
in a union
as the former contains a constructor.
(Although allowed in C++11 this is not supported in VS2013).
来源:https://stackoverflow.com/questions/22764587/compilation-error-for-union