对于 C++03,这两个问题的答案都是“否”。自从
9.5 联合[class.union]
1
...
An object of a class with a non-trivial default constructor (12.1), a non-trivial copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor
can an array of such objects. If a union contains a static data member, or a member of reference type,
the program is ill-formed.
您仍然可以将构造函数添加到将初始化指针而不是类 ctor 的联合中。
但是,在 C++11 中,您可以为联合提供自己的构造函数,这应该实现正确的方法来复制具有非平凡构造函数的类。
9.5 联合[class.union]
2 A union can have member functions (including constructors and destructors), but not virtual (10.3) functions. A union shall not have base classes. A union shall not be used as a base class. If a union contains a non-static data member of reference type the program is ill-formed. At most one non-static data member of a union may have a brace-or-equal-initializer. [Note: If any non-static data member of a union has a non-trivial default constructor (12.1), copy constructor (12.8), move constructor (12.8), copy assignment operator (12.8), move assignment operator (12.8), or destructor (12.4), the corresponding member function of the union must be
user-provided or it will be implicitly deleted (8.4.3) for the union. — end note ]
您可能还对以下文章感兴趣
http://cpp11standard.blogspot.com/2012/11/c11-standard-explained-1-unrestricted.html