草庐IT

union-click

全部标签

c++ - 如果只有一个成员没有默认构造函数,为什么 union 有一个已删除的默认构造函数?

N3797::9.5/2[class.union]说:Ifanynon-staticdatamemberofaunionhasanon-trivialdefaultconstructor(12.1),copyconstructor(12.8),moveconstructor(12.8),copyassignmentoperator(12.8),moveassignmentoperator(12.8),ordestructor(12.4),thecorrespondingmemberfunctionoftheunionmustbeuser-providedoritwillbeimplic

c++ - 如果只有一个成员没有默认构造函数,为什么 union 有一个已删除的默认构造函数?

N3797::9.5/2[class.union]说:Ifanynon-staticdatamemberofaunionhasanon-trivialdefaultconstructor(12.1),copyconstructor(12.8),moveconstructor(12.8),copyassignmentoperator(12.8),moveassignmentoperator(12.8),ordestructor(12.4),thecorrespondingmemberfunctionoftheunionmustbeuser-providedoritwillbeimplic

c++ - 在类中使用 union

我看到一些代码如下:classA{private:union{B*rep;A*next;};//novariablesofthisanonymousdefined!voidfunc(){A*p=newA;p->next=NULL;//whyphasamembervariableof'next'?}};我已经用VS2010编译了上面的代码,没有任何错误。问题来了,为什么p有成员变量'next'?union{B*rep;A*next;};据我所知,这是一个匿名union,甚至没有定义变量。我们怎样才能像那样访问这个union内部的成员变量? 最佳答案

c++ - 在类中使用 union

我看到一些代码如下:classA{private:union{B*rep;A*next;};//novariablesofthisanonymousdefined!voidfunc(){A*p=newA;p->next=NULL;//whyphasamembervariableof'next'?}};我已经用VS2010编译了上面的代码,没有任何错误。问题来了,为什么p有成员变量'next'?union{B*rep;A*next;};据我所知,这是一个匿名union,甚至没有定义变量。我们怎样才能像那样访问这个union内部的成员变量? 最佳答案

c++ - 在 C++ 中静态初始化匿名 union

我正在尝试在VisualStudio2010中静态初始化以下结构:structData{intx;union{constData*data;struct{intx;inty;};};};以下失败,errorC2440:'initializing':cannotconvertfrom'Data*'to'char'。staticDatad1;staticDatad={1,&d1};staticDatad2={1,{1,2}};我发现了一些可以正确初始化的方法的引用,但它们都不能在VS2010中工作。有什么想法吗? 最佳答案 ISOC++

c++ - 在 C++ 中静态初始化匿名 union

我正在尝试在VisualStudio2010中静态初始化以下结构:structData{intx;union{constData*data;struct{intx;inty;};};};以下失败,errorC2440:'initializing':cannotconvertfrom'Data*'to'char'。staticDatad1;staticDatad={1,&d1};staticDatad2={1,{1,2}};我发现了一些可以正确初始化的方法的引用,但它们都不能在VS2010中工作。有什么想法吗? 最佳答案 ISOC++

c++ - 空结构和匿名 union 怪事

在fedora-linux上使用gcc4.8.2和llvm/clang3.4将我的代码编译为C++11,我得到了我无法真正理解的奇怪结果解释...这是一个类似的程序fedora。#includeusingnamespacestd;structA{};structC{};structB1:A{union{Aa;};};structB2:A{union{Cc;};};intmain(){coutsizeof(B1)=2和sizeof(B2)=1但是为什么尺寸不同?其实我有一个“为什么”的想法,但我想找到确切的解释或C++规则。 最佳答案

c++ - 空结构和匿名 union 怪事

在fedora-linux上使用gcc4.8.2和llvm/clang3.4将我的代码编译为C++11,我得到了我无法真正理解的奇怪结果解释...这是一个类似的程序fedora。#includeusingnamespacestd;structA{};structC{};structB1:A{union{Aa;};};structB2:A{union{Cc;};};intmain(){coutsizeof(B1)=2和sizeof(B2)=1但是为什么尺寸不同?其实我有一个“为什么”的想法,但我想找到确切的解释或C++规则。 最佳答案

c++ - Boost Variant 本质上是 C/C++ 中的 union ?

我想知道c/c++中的BoostVariant和union数据类型之间有什么区别。我知道union数据类型占用相同的内存位置,并且内存区域中最大的数据类型占用使用的内存总量,例如unionspace{charCHAR;floatFLOAT;intINTEGER;}S;应该占用4个字节的内存,因为int和float是最大且相等的大小。BoostVariant和union数据类型在其他方面是否有相同点和不同点?我也知道BoostVariant可以采用任何数据类型,并且它允许数据类型“多态性”(如果我误用了OOP主题词,请纠正我)。因此,union数据类型也是一种多态性吗?

c++ - Boost Variant 本质上是 C/C++ 中的 union ?

我想知道c/c++中的BoostVariant和union数据类型之间有什么区别。我知道union数据类型占用相同的内存位置,并且内存区域中最大的数据类型占用使用的内存总量,例如unionspace{charCHAR;floatFLOAT;intINTEGER;}S;应该占用4个字节的内存,因为int和float是最大且相等的大小。BoostVariant和union数据类型在其他方面是否有相同点和不同点?我也知道BoostVariant可以采用任何数据类型,并且它允许数据类型“多态性”(如果我误用了OOP主题词,请纠正我)。因此,union数据类型也是一种多态性吗?