zoukankan      html  css  js  c++  java
  • struct enum union区别

    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# 小算法1
    函数 y=x^x的分析
    随机数
    对拍
    Cube Stack
    Permutation
    一笔画问题
    康托展开&&康托逆展开
    待完成
    小错误 17/8/10
  • 原文地址:https://www.cnblogs.com/jobs1/p/11040237.html
Copyright © 2011-2022 走看看