C++ struct,union和enum

來源:互聯網
上載者:User
struct

struct是一組資料元素一個名字,這些資料元素,作為成員,可以有不同類型和不同長度。C++聲明struct文法如下:

struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;

簡單樣本如下:

struct product {
int weight;
float price;
} ;

product apple;
product banana, melon;

聲明struct並定義多個執行個體:

struct product {
int weight;
float price;
} apple, banana, melon;

完整struct例子:

// example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
string title;
int year;
} mine, yours;

void printmovie (movies_t movie);

int main ()
{
string mystr;

mine.title = "2001 A Space Odyssey";
mine.year = 1968;

cout << "Enter title: ";
getline (cin,yours.title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> yours.year;

cout << "My favorite movie is:\n ";
printmovie (mine);
cout << "And yours is:\n ";
printmovie (yours);
return 0;
}

void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
 struct和指標
struct movies_t {
string title;
int year;
};

movies_t amovie;
movies_t * pmovie;
pmovie = &amovie;

完整樣本如下:

// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
string title;
int year;
};

int main ()
{
string mystr;

movies_t amovie;
movies_t * pmovie;
pmovie = &amovie;

cout << "Enter title: ";
getline (cin, pmovie->title);
cout << "Enter year: ";
getline (cin, mystr);
(stringstream) mystr >> pmovie->year;

cout << "\nYou have entered:\n";
cout << pmovie->title;
cout << " (" << pmovie->year << ")\n";

return 0;
}
 內部struct
struct movies_t {
string title;
int year;
};

struct friends_t {
string name;
string email;
movies_t favorite_movie;
} charlie, maria;

friends_t * pfriends = &charlie;

使用如下:

charlie.name
maria.favorite_movie.title
charlie.favorite_movie.year
pfriends->favorite_movie.year
union

union允許以不同的資料類型訪問相同的記憶體,因為他們實際上是在記憶體中的相同位置,它的聲明和struct差不多,但功能完全不同:

union union_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;

union聲明的所有元素在記憶體中佔有相同的物理空間,其大小是該聲明的最大元素的大小:

union mytypes_t {
char c;
int i;
float f;
} mytypes;

 

每一個不同的資料類型。由於所有的元素都指的是記憶體中的同一位置,修改的內容之一,會影響其他元素的內容。

 匿名union

struct和正常union

struct {
char title[50];
char author[50];
union {
float dollars;
int yen;
} price;
} book;

struct和匿名union

struct {
char title[50];
char author[50];
union {
float dollars;
int yen;
};
} book;

第一種類型的訪問方式,如下:

book.price.dollars
book.price.yen

第二種類型的訪問方式,如下:

book.dollars
book.yen

 

enum

  定義枚舉類型的基本文法:

enum enumeration_name {  value1,  value2,  value3,  .  .} object_names;

例如:

enum Week {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};

其中SUNDAY = 0,MONDAY = 1……SATURDAY = 6。也就是說,第1個枚舉值代表0,第2個枚舉值代表1,這樣依次遞增1。

也可以在定義時,直接指定某個或某些枚舉值的數值,例如:

 enum Week {MONDAY = 1,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY};

其中MONDAY等於1,這樣,TUESDAY就將等於2,直接到SUNDAY等於7。

 

聲明枚舉變數:

Week today = TUESDAY;

 

枚舉值,我們就稱為格舉常量,因為它一經定義以後,就不可再改變,以下用法是錯誤的!

TUESDAY = 10; //錯誤!我們不能改變一個枚舉值的數值。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.