C++ 數(shù)據(jù)類(lèi)型
struct
/** * 這里的student相當(dāng)于一個(gè)對(duì)象 * name|age是student的屬性 */ struct student { string name; int age; }; // 初始化 student stu1; student.name = "XiaoMing"; student.age = 18; // ----------------------------------------- // 指針獲取student屬性 student *studentPointer = &student; string name = studentPointer->name; // -----------------------------------------
union:
/** * union中的屬性共用一個(gè)內(nèi)存 * 例:當(dāng)i=97時(shí),輸出的c 為'a' */ union myType { int i; char c; }; // ----------------------------------------- /** * 在struct結(jié)構(gòu)體中,可以匿名使用union */ struct book { char title[50]; char author[50]; union { float dollars; int yen; } }; // 獲取book中的匿名屬性 book book1; book1.dollars; // -----------------------------------------
enum:
/** * 常規(guī)枚舉 * 若不設(shè)置black值,則會(huì)默認(rèn)0;后續(xù)值為前一個(gè)+1 */ enum colors_t { black=1, blue, green }; // 枚舉使用 colors_t myColor = blue; cout << myColor << endl; // 輸出對(duì)應(yīng)的指為2 // ----------------------------------------- /** * class類(lèi)型的枚舉 */ enum class Colors { black, blue, green }; // enum class 使用 Colors myColor = Colors::black; // ----------------------------------------- /** * 枚舉指定基礎(chǔ)類(lèi)型 * todo 還不太確定這個(gè)有什么用 */ enum class EyeColor : char { blue, green, brown }

浙公網(wǎng)安備 33010602011771號(hào)