奇怪的行为类对象里面联盟 [英] Strange Behaviour Class Objects Inside Union
问题描述
您好我想知道以下代码的原因
Hi I wanted know the reasons of the following code
void main()
{
class test
{
public:
test(){}
int k;
};
class test1
{
public:
test1(){}
int k;
};
union Test
{
test t1;
test1 t2;
};
}
对于上面的代码,它给出错误 error C2620: Test':member't1'has user-defined constructor or non-trivial default constructor
For the Above code it gives error "error C2620: union 'Test' : member 't1' has user-defined constructor or non-trivial default constructor"
class test
{
public:
//test(){}
int k;
};
class test1
{
public:
//test()1{};
int k;
};
union Test
{
test t1;
test1 t2;
};
对于上述,没有错误。
我想知道原因。
在预先感谢。 :
推荐答案
根据C ++标准(§9.5.1,以及其他答案):
According to the C++ standard (§9.5.1, cited as well in other answers):
联合可以有成员函数(包括构造函数和析构函数),但不能有虚函数。联合不应有基类。联合不应用作基类。具有非平凡构造函数,非平凡复制构造函数,非平凡析构函数或非平凡复制赋值运算符的类的对象不能是联合的成员,也不能是此类对象的数组。如果一个union包含一个静态数据成员或一个引用类型的成员,那么程序就会失败。
A union can have member functions (including constructors and destructors), but not virtual functions. A union shall not have base classes. A union shall not be used as a base class. An object of a class with a non-trivial constructor, a non-trivial copy-constructor, a non-trivial destructor, or a non-trivial copy assignment operator 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 a reference type, the program is ill-formed.
到关于POD类型的维基百科文章,其中规定:
C ++中的POD类型定义为标量类型或POD类。 POD类没有用户定义的副本赋值运算符,没有用户定义的析构函数,也没有非本身为POD的非静态数据成员。此外,POD类必须是聚合,这意味着它没有用户声明的构造函数,没有私有和受保护的非静态数据,没有基础和没有虚拟函数。该标准包括关于POD在C ++中必须如何表现的声明。
A POD type in C++ is defined as either a scalar type or a POD class. POD class has no user-defined copy assignment operator, no user-defined destructor, and no non-static data members that are not themselves PODs. Moreover, POD class must be an aggregate, meaning it has no user-declared constructors, no private nor protected non-static data, no bases and no virtual functions. The standard includes statements about how PODs must behave in C++.
和
在某些上下文中,C ++只允许使用POD类型。 例如,C ++中的联合不能包含具有虚拟函数的类,或者非平凡构造函数或析构函数。这个限制是因为编译器不知道应该为一个联合调用哪个构造函数或析构函数。
In certain contexts, C++ allows only POD types to be used. For example, a union in C++ cannot contain a class that has virtual functions, or nontrivial constructors or destructors. This restriction is imposed because the compiler cannot know which constructor or destructor should be called for a union.
第二段的第一句可能会让你认为C ++只允许POD类型成为联合的一部分。这不完全是这样的,因为它允许具有私有成员的类作为联合的一部分:
The first sentence of the second paragraph might make you think C++ only allows POD types to be part of a union. This isn't exactly the case as it allows a class with private members to be part of a union:
#include <iostream>
using namespace std;
class test1
{
int i;
};
class test2
{
int i;
};
union test
{
test1 t1;
test2 t2;
};
int main()
{
cout << __is_pod(test1) << endl;
cout << __is_pod(test2) << endl;
cout << __is_pod(test) << endl;
return 0;
}
使用MSVC ++编译的程序打印出来:
The program above compiled with MSVC++ prints out:
0
0
1
这篇关于奇怪的行为类对象里面联盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!