CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
英特尔®游戏设计大赛100美元现金周周送 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

麻烦您帮个忙。---先行谢过。

楼主mituzhishi(慎独)2003-11-01 18:42:30 在 C/C++ / C语言 提问

请告知我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

相关问题

  • 求各位高人帮忙。先行谢过!
  • 高手请进。先行谢过。
  • JavaScript小问题,先行谢过!
  • 第一次在贵地提问题, 帮帮忙! NT4 server 怎样 起ftp server, 先行谢过
  • 请问ORACLE的Developer/2000在哪儿可以找到?先行谢过!
  • dreamfan请给我一份图标库文件,先行谢过?
  • 万分火急,请求帮助!先行谢过。
  • ~~~只为解决放大、缩小~~~先行谢过了!!!
  • 数据库连接问题/??先行谢过!
  • 简单的触发器问题,更新时另外一个表的相应字段也跟着更新!请各位帮忙,先行谢过

关键词

  • 内存
  • 联合
  • cpp
  • 结构
  • mailaddr
  • liuchuntao
  • liufengyun
  • uniontest
  • tele
  • 变量

得分解答快速导航

  • 帖主:mituzhishi
  • zlqian
  • sunjx119
  • Sodier
  • Andy84920
  • sandrowjw

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo