zoukankan      html  css  js  c++  java
  • C和C++中结构体(struct)、联合体(union)、枚举(enum)的区别

     

    C++对C语言的结构、联合、枚举 这3种数据类型进行了扩展。

    1、C++定义的结构名、联合名、枚举名 都是 类型名,可以直接用于变量的声明或定义。即在C++中定义变量时不必在结构名、联合名、枚举名 前加上前缀struct、union、enum。

    例如有如下头文件(head.h)

    //head.h
    
    enum color {red,blak,white,blue,yellow};
    
    struct student {char name[6]; int age; int num;};
    
    union score {int i_sc; float f_sc;}; 

    在C中使用的使用的方法

    #include "head.h"
    int main(void)
    {
        enum color en_col;
        struct student st_stu;
        union score un_sc;
        
        //....
        return 0;
    }

    在C++中使用的使用的方法

    #include "head.h"
    
    int main(void)
    {
        color en_col;
        student st_stu;
        score un_sc;
        
        //....
        return 0;
    }

    在C语言中定义这3种变量显得很麻烦,在C中通常使用typedef来达到和C++一样的效果

    //example.c
    
    typedef enum _color {red,blak,white,blue,yellow}color;
    typedef struct _student {char name[6]; int age; int num;}student; 
    typedef union _score {int i_sc; float f_sc;} score;
    
    int main(void)
    {
        color en_col;
        student st_stu;
        score un_sc;
        
        //....
        return 0;
    }

    2、C++中的结构体 和 联合体 中可以定义函数。

    下面是一个简单的例子

    //example2.cpp
    
    #include <iostream>
    using namespace std;
    
    
    struct student 
    {
        char name[6]; 
        int age; 
        char* GetName(void){return name;};
        int GetAge(void){return age;};
    };
    union score {
    int i_sc; float f_sc; int GetInt(void){return i_sc;}; float GetFloat(void){return f_sc;}; }; int main(void) { student st_stu = {"Lubin", 27}; score un_sc = {100}; cout << st_stu.GetName() << endl; cout << st_stu.GetAge() << endl; cout << un_sc.GetInt() << endl; return 0; }
    /* 输出结果
    Lubin
    27
    100
    */

     2.1 C++中struct 和 class 的区别

     在C++中struct也是一种类,他与class具有相同的功能,用法完全相同。

    唯一的区别就是:在没有指定成员的访问权限时,struct中默认为public权限,class中默认为private权限。

    2.2 C++中的 union 和 class 的区别

    union可以定义自己的函数,包括 constructor 以及 destructor。
    union支持 public , protected 以及 private 权限。


    读者看到这可能会问,要是这样的话,union与class还有什么区别吗?区别当然还是有的


    1. union不支持继承。也就是说,union既不能有父类,也不能作为别人的父类。

    2. union中不能定义虚函数。

    3.在没有指定成员的访问权限时,union中默认为public权限

    4.union中的成员类型比class少,具体见2.2.1节

    2.2.1 C++中的 union 不能存放的成员类型

    联合里面的东西共享内存,所以静态、引用都不能用,因为他们不可能共享内存。

    不是所有类都能作为union的成员变量,如果一个类,包括其父类,含有自定义的constructor,copy constructor,destructor,copy assignment operator(拷贝赋值运算符), virtual function中的任意一个,

    那么这种类型的变量不能作为union的成员变量,因为他们共享内存,编译器无法保证这些对象不被破坏,也无法保证离开时调用析够函数。

    2.2.2 C++中的 union 的匿名联合(屠龙之术 - 一辈子都可能不会用到)

    如果我们在定义union的时候没有定义名字,那么这个union被称为匿名union(anonymous union)。

    匿名联合仅仅通知编译器它的成员变量共同享一个地址,并且变量本身是直接引用的,不使用通常的点号运算符语法.

    匿名union的特点如下:

    1. 匿名union中不能定义static变量。
    2. 匿名union中不能定义函数。
    3. 匿名union中不支持 protected 以及 private 权限。
    4. 在全局域以及namespace中定义的匿名union只能是static的,否则必须放在匿名名字空间中。

    -------------------------------------------------------------------------------------------------------------------------------------------------------------

    参考资料:

    <<C++面向对象程序设计(第二版)>>

    http://blog.csdn.net/marrco2005/article/details/1657160

    http://zwkufo.blog.163.com/blog/static/25882512010729101816347/

    http://blog.csdn.net/syhhl007/article/details/4678786

  • 相关阅读:
    POJ2888 Magic Bracelet [矩阵快速幂+Burnside+欧拉函数]
    数列的 GCD [计数问题]
    com组件的注册
    WCF 传输和接受大数据
    数据库中已存在名为 'View_Business' 的对象。
    windows 两个用户,默认其中一个用户登录
    用C#读取,写入ini文件
    小心得,关于串口
    未能加载文件或程序集"Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad
    无法激活服务,因为它不支持 ASP.NET 兼容性
  • 原文地址:https://www.cnblogs.com/LubinLew/p/struct_enum_union_in_cpp.html
Copyright © 2011-2022 走看看