麻烦您帮个忙。---先行谢过。
请告知我struct和union的区别,最好举一个例子。 问题点数:20、回复次数:11Top
1 楼mituzhishi(慎独)回复于 2003-11-01 19:44:48 得分 0
我是楼主,在下面的程序中我用了struct和union,出现了错误,去掉union时就没有错误了,请高手告知原因。#include<iostream>
#include<string>
using namespace std;
struct information
{ int tele_no;
string addr;
union
{
string mailAddr;
};
}liufengyun,liuchuntao;
int main()
{ liufengyun.tele_no =8745122;
liuchuntao.tele_no =8232283;
liufengyun.addr ="Jingzhou";
liuchuntao.addr ="Handan";
liuchuntao.mailAddr ="mituzhishi@hotmail.com";
cout<<"Liu fengyun and Liu chuntao's telephone numbers are:"<<endl;
cout<<liufengyun.tele_no<<" "<<liuchuntao.tele_no<<endl;
cout<<"Liu fengyun and Liu chuntao's addresses are:"<<endl;
cout<<liufengyun.addr<<" "<<liuchuntao.addr<<endl;
cout<<"Liu chuntao's E-mail is:"<<endl;
cout<<liuchuntao.mailAddr<<endl;
return 0;
}
Top
2 楼zlqian(zlqian)回复于 2003-11-01 19:58:56 得分 0
test.cpp:9: member `std::string information::<anonymous union>::mailAddr' with
constructor not allowed in union
test.cpp:9: member `std::string information::<anonymous union>::mailAddr' with
destructor not allowed in union
test.cpp:9: member `std::string information::<anonymous union>::mailAddr' with
copy assignment operator not allowed in union
bash-2.05b$ g++ test.cpp
for example:
union test
{
Top
3 楼zlqian(zlqian)回复于 2003-11-01 20:03:09 得分 5
test.cpp:9: member `std::string information::<anonymous union>::mailAddr' with
constructor not allowed in union
test.cpp:9: member `std::string information::<anonymous union>::mailAddr' with
destructor not allowed in union
test.cpp:9: member `std::string information::<anonymous union>::mailAddr' with
copy assignment operator not allowed in union
bash-2.05b$ g++ test.cpp
for example:
union test
{
int i;
char j;
};
i and j will in the same place in the memory! but both int and char are native type. It is nothing !
union test
{
int i;
string j;
};
j is a class, when it is inited, constructor must be invoked. but i and j is in the same memory. any change from i to j will invoke the constructor.It will sollow down the speed!
Top
4 楼mituzhishi(慎独)回复于 2003-11-01 20:50:30 得分 0
Dear zlqian,
I am sorry,I can not quite understand your explanation.Please speak in Chinese next time.
Thank you very much!Top
5 楼sunjx119(睿锐)回复于 2003-11-01 23:29:16 得分 2
结构:在结构中各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和。
联合:各成员共享一段内存空间, 一个联合变量的长度等于各成员中最长的长度。但这里所谓的共享不是指把多个成员同时装入一个联合变量内, 而是指该联合变量可被赋予任一成员值,但每次只能赋一种值, 赋入新值则冲去旧值。
Top
6 楼Sodier(逍遥神剑)回复于 2003-11-02 01:29:36 得分 10
#include<iostream>
#include<string>
using namespace std;
struct information
{ int tele_no;
string addr;
union
{
char *mailAddr;//联合中的数据类型应该有确定的内存空间,string长度不定
}aa;//声明一个数据对象
}liufengyun,liuchuntao;
int main()
{ liufengyun.tele_no =8745122;
liuchuntao.tele_no =8232283;
liufengyun.addr ="Jingzhou";
liuchuntao.addr ="Handan";
liuchuntao.aa.mailAddr ="mituzhishi@hotmail.com";//用指针初始化,用aa引用
cout<<"Liu fengyun and Liu chuntao's telephone numbers are:"<<endl;
cout<<liufengyun.tele_no<<" "<<liuchuntao.tele_no<<endl;
cout<<"Liu fengyun and Liu chuntao's addresses are:"<<endl;
cout<<liufengyun.addr<<" "<<liuchuntao.addr<<endl;
cout<<"Liu chuntao's E-mail is:"<<endl;
cout<<liuchuntao.aa.mailAddr<<endl;//搂主不能对mailAddr直接用liuchuntao
return 0;//结构名直接引用,注意层次关系
}
联合中的数据共享一块内存,如果里面有多个数据对象,不能对他们同时引用。
对联合初始化时,只能对第一个数据对象初始化,而不能对其他数据类型初始化,
例如:
union aa
{
int a;
float b;
}AA;
AA={10};//正确
AA={0.1};//错误
当然,如果象下面这样初始化肯定是对的:
AA.a=10;
AA.b=0.1;
Top
7 楼sandrowjw(我的小猫照片给弄坏了,心都碎了)回复于 2003-11-07 17:00:34 得分 0
union中不能有类,否则编译器无法知道调用哪个类的构造函数。Top
8 楼Andy84920(你也不懂)回复于 2003-11-07 17:12:00 得分 1
union中不能有类,否则编译器无法知道调用哪个类的构造函数。
为什么说编译器无法知道调用哪个类的构造函数呢?
好像Sodier(逍遥神剑)说的还不错:
联合中的数据类型应该有确定的内存空间,string长度不定.
Top
9 楼sandrowjw(我的小猫照片给弄坏了,心都碎了)回复于 2003-11-10 09:46:43 得分 2
举个例子,应该输出几?unknown或者3?实际上这个程序编译是不能够通过的,因为编译器无法知道是否要初始化union的那块内存。
#include <iostream>
class Don{
int i;
Don()
{ i = 3; }
};
struct Boo{
union{
int i;
Don p;
}
};
void main()
{
Boo b;
std::cout<<b.i;
}Top
10 楼sandrowjw(我的小猫照片给弄坏了,心都碎了)回复于 2003-11-10 09:50:59 得分 0
有一种情况是可以的,就是把Don的构造函数去掉,这样的话union就不必去关心初始化。
我上面的描述不是很确切,应该是没有构造函数并且构造函数是non-trivial的类是可以写进union的。
(另外,上面少了一个分号:p)Top
11 楼bluedodo(笑三少)回复于 2003-11-10 10:53:13 得分 0
从没钻过这个牛角尖,不过楼上说的有理Top




