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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM