複合類型
维库,知识与思想的自由文库
|
在電腦科學中,複合類型是一種資料類型,它可以原始類型和其它的複合類型所構成。構成一個複合類型的動作,又稱作組合。 [编辑] C/C++
在 C++ 裡, 注意類別和 C++ 的新關鍵字
例如:
struct Account {
int account_number;
char *first_name;
char *last_name;
float balance;
};
定義一個稱為
typedef struct Account_ {
int account_number;
char *first_name;
char *last_name;
float balance;
} Account;
在 C++ 中,並不需要 其它例子,一個使用了浮點數資料類型的三維向量複合類型,可如此建立︰
struct Vector {
float x;
float y;
float z;
};
一個以 同樣地,一個顏色結構可如此建立︰
struct Color {
int red;
int green;
int blue;
};
在三維圖像中,必須經常不斷追蹤每一個頂點的位置和顏色。可以使用之前所建立的
struct Vertex {
Vector position;
Color color;
};
以同樣的格式建立一個 v.position.x = 0.0; v.position.y = 1.5; v.position.z = 0.0; v.color.red = 128; v.color.green = 0; v.color.blue = 255; [编辑] 原始子類型剛開始使用的
struct ifoo_old_stub {
long x, y;
};
struct ifoo_version_42 {
long x, y, z;
char *name;
long a, b, c;
};
void operate_on_ifoo(struct ifoo_old_stub *);
struct ifoo_version_42 s;
. . .
operate_on_ifoo(&s);
將可正確運作。 [编辑] 參閱 |


