在struct结构体中使用未命名的union有什么作用呢? 如题,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 struct可以定义一个包含多个不同变量的类型,每一个变量在内存中占有自己独立的内存空间,可以同时存储不同类型的数据。union也可以定义一个包含多个不同变量类型,但这些变量只共有同一个内存空间,每次只能使用其中的一种变量存储数据。这样 struct中的union规定了这块为共享空间,其余部分为独立空间。 #include "stdafx.h"#include <iostream>#include <string>#include <atlstr.h>using namespace std;struct node{ union{ int i; CString str1; };};int main(){ node n; node m; n.str1="abc"; n.i=10; m.str1="ABC"; m.i=100; cout<<n.str1<<n.i<<endl; cout<<m.str1<<m.i<<endl; cin>>m.i;}为什么程序在VS2005 Debug编译时提示:1>.\try.cpp(12) : error C2621: 成员“node::str1”(属于联合“node::<unnamed-tag>”)具有复制构造函数 而且有必要在struct内仅包含一个union,那不是直接用union就可以了???? struct 还可以包含其它你不想共享内存空间的量啊 这我知道,是为了拓展嘛我的意思是如果已经通过编译,不准备再进行拓展了如:struct node{ union{ int i; int j; };};是不是可以等价的定义为:union node{ int i; int j;}; 定义一个CString类型的对象要包含什么头文件? 你怎么定义随便了嘛,要给分的话都给 BUbuWander这个id吧 钩子如何截获 数学表达式问题? vc处理jpeg图像 CTreeCtrl的条目长度超过边框 闪屏 类型匹配的问题!!!!error C2440 CDialog的 mousemove事件未被激发,可能是什么原因? CSplitterWnd的使用问题 【有关CListBox】 改变CListBox的背景 怎么头像可以是动态的? 如何将BYTE ID[2],赋值给UNS32 id; 请问新建的DLL工程为什么会产生Release/mydll.lib文件? 关于CComboBox问题,请大家指点
union也可以定义一个包含多个不同变量类型,但这些变量只共有同一个内存空间,每次只能使用其中的一种变量存储数据。这样 struct中的union规定了这块为共享空间,其余部分为独立空间。
#include <iostream>
#include <string>
#include <atlstr.h>
using namespace std;
struct node{
union{
int i;
CString str1;
};
};int main()
{
node n;
node m;
n.str1="abc";
n.i=10;
m.str1="ABC";
m.i=100;
cout<<n.str1<<n.i<<endl;
cout<<m.str1<<m.i<<endl;
cin>>m.i;
}为什么程序在VS2005 Debug编译时提示:
1>.\try.cpp(12) : error C2621: 成员“node::str1”(属于联合“node::<unnamed-tag>”)具有复制构造函数
如:
struct node{
union{
int i;
int j;
};
};
是不是可以等价的定义为:
union node{
int i;
int j;
};