[英]C++ union definition
Is is possible to define a union like we can do for named namespace ? 是否可以像我们对命名名称空间那样定义一个联合? I mean can we declare a first type in the union and then in another file add another type to the union ? 我的意思是我们可以在联合中声明第一个类型,然后在另一个文件中向联合中添加其他类型吗?
For now, I have a based class that contains a union of bit types (unsigned int and other which correspond to each derived class). 现在,我有一个基类,其中包含位类型的并集(无符号int和其他与每个派生类相对应的类型)。 I would like to split them in the derive class and construct bit by bit this union. 我想将它们拆分到派生类中,并一点一点地构造这个联合。
No, such as for structs/classes
, enums
. 不,例如对于structs/classes
, enums
。
n3337 7.3/1 n3337 7.3 / 1
A namespace is an optionally-named declarative region. 名称空间是一个可选命名的声明性区域。 The name of a namespace can be used to access entities declared in that namespace; 名称空间的名称可用于访问在该名称空间中声明的实体。 that is, the members of the namespace. 即名称空间的成员。 Unlike other declarative regions , the definition of a namespace can be split over several parts of one or more translation units. 与其他声明性区域不同 , 名称空间的定义可以分为一个或多个翻译单元的多个部分。
No. How could it even be made to work (not that it's desirable), with separate compilation. 否。如何通过单独的编译使它工作(不是很理想)。 Consider something like: 考虑类似:
union U { int i; double d; }
void
f()
{
U aU;
g( &aU );
// ...
}
How much space is the compiler to allocated on the stack for aU
. 编译器要在堆栈上为aU
分配多少空间。 Given that a DLL not yet written contains: 鉴于尚未编写的DLL包含:
union U { char buff[1000]; };
void
g( U* aU )
{
// Use all 1000 bytes...
}
(Note that according to the standard, the above is undefined behavior. What you are asking would require it to work.) (请注意,根据标准,以上内容是未定义的行为。您所要求的将要求它起作用。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.