2020年7月3日 星期五

讀書心得 - C++ Primer (5th Edition) - Chapter 2 (1) - Primitive Types



C++ 原生內建的資料型態 ( Primitive Types )

    C++ 定義了一些算術用的資料型態 ( Arithmetic Types ) 和一個特殊的資料型態 void。Arithmetic Types : Integer, Character, Boolean, Floating Point, Double Floating Point, Wide Character。

    下面為一些資料型態轉換 :
    bool b = 42;            // b is true
    int i = b;              // i has value 1
    i = 3.14;               // i has value 3
    double pi = i;          // pi has value 3.0
    unsigned char c = -1;   // assuming 8-bit chars, c has value 255
    signed char c2 = 256;   // assuming 8-bit chars, the value of c2 is undefined

Initialization

    各種初始 :
    int units_sold1 = 0;
    int units_sold2 = {0};
    int units_sold3{0};
    int units_sold4(0);
    {} 跟 () 的不同 :
    long double ld = 3.1415926536;
    int a{ld}, b = {ld};    // error: narrowing conversion required
    int c(ld), d = ld;      // ok: but value will be truncated

Declaration 跟 Definition

    為了支援多個檔案的編譯,cpp 區分了 Declaration 跟 Definition。Declaration 代表著跟整個程式宣告一個帶有資料型態的名子,任何檔案都可以去作存取。Definition 則是創造一個物件實體去對應那個帶有資料型態的名子。
    如果想要只宣告名子(代表實際物件會被在其他檔案給實現),則會在宣告時加上 extern,有了 extern 就代表你不能下 Definition。
    extern int i;              // declares but does not define i
    int j;                     // declares and defines j
    extern double pi = 3.1416; // error: ‘pi’ has both ‘extern’ and initializer

Compound Types - References

    Reference 等於創造額外的一個名子給一個物件。所以下面第 refVal2, refVal4 會 error,因為你不能給不存在的物件 ( 或 rvalue ) 取名。References must be initialized.
    int ival = 1024;
    int &refVal = ival;     // refVal refers to (is another name for) ival
    int &refVal2;           // error: a reference must be initialized
    int &refVal3 = refVal;  // ok: refVal3 is bound to the object to which refVal is bound, i.e., to ival
    int &refVal4 = 10;      // error: initializer must be an object

Compound Types - Pointers

    Pointer為一個 "指向" 物件。類似 References 但卻是一個物件,所以可以不用被 initialized,也可以用來複製、分配。下面的 & 是作為 operator,用來取物件的實際位置,與上面提到用來宣告資料型態的的 & 有著不同的意義。
    double dval;
    double *pd = &dval; // ok: initializer is the address of a double
    double *pd2 = pd;   // ok: initializer is a pointer to double
    int *pi = pd;       // error: types of pi and pd differ
    pi = &dval;         // error: assigning the address of a double to a pointer to int
    下面的 * 作為 operator,放在 pointer 前面等於取其指向的該物件
    *p = 0;             // * yields the object; we assign a new value to ival through p
    std::cout << *p;    // prints 0

更多 References & Pointers 範例

    double dval;
    double *pd = &dval;  // ok: initializer is the address of a double
    double *pd2 = pd;    // ok: initializer is a pointer to double
    int *pi = pd;        // error: types of pi and pd differ
    pi = &dval;          // error: assigning the address of a double to a pointer to int

    int i = 42;
    int &r = i;          // & follows a type and is part of a declaration; r is a reference
    int *p;              // * follows a type and is part of a declaration; p is a pointer
    p = &i;              // & is used in an expression as the address-of operator
    *p = i;              // * is used in an expression as the dereference operator
    int &r2 = *p;        // & is part of the declaration; * is the dereference operator
相關文章 :
上一篇 :
下一篇 :

0 意見:

張貼留言

Popular Posts