2020年7月9日 星期四

讀書心得 - C++ Primer (5th Edition) - Chapter 2 (2) - Const



修飾詞 : Const

    如果我們想要一個不可被改變的參數,這時就會需要這個修飾詞 ( Qualifier ) const。
    const int bufSize = 512;  // input buffer size
    bufSize = 1024;           // error: attempt to write to const object

Const + Reference

    Const + Reference 代表 Reference to const,等於你不能使用這個 Reference 去改變物件。沒有 const 修飾詞 ( plain object ) 的 reference 不可以 bind 有 const 修飾詞的物件,但反之有 const 修飾詞的 reference 可以 bind 無 const 修飾詞的物件。
    int i = 1;
    const int &r = i;  // we can bind a const int& to a plain int object
    i = i + 1;         // "r" will be 2 too.
    r = r + 1;         // error: assignment of read-only reference "r"
    int &j = r;        // error: binding ‘const int’ to reference of type ‘int&’ discards qualifiers

Const + Pointer ( const type * var )

    跟 Reference 一樣 Pointer to const,等於你不能使用這個 Pointer 去改變物件。沒有 const 修飾詞 ( plain object ) 的 Pointer 不可以 bind 有 const 修飾詞的物件,但反之有 const 修飾詞的 Pointer 可以 bind 無 const 修飾詞的物件。
    const double pi = 3.14;     // pi is const; its value may not be changed
    double *ptr = π          // error: ptr is a plain pointer
    const double *cptr = π   // ok: cptr may point to a double that is const
    *cptr = 42;                 // error: cannot assign to *cptr

Const + Pointer ( type * const var )

    但 Pointer 跟 Reference 不一樣,本身也是物件,所以可以 const pointer。const pointer 則必須被 initialized。但因為是 const pointer 而不是 const 指向物件,所以可以用 const pointer 去改變物件。
    int numb = 0;
    int * const cptr1 = &numb;  // cptr1 will always point to numb
    int * const cptr2;          // error: uninitialized const 'cptr2'
    *cptr1 = 5;                 // numb will be 5 too.

top-level/lower-level const

    像 pointer 的 const 就有兩種,所以稱 const pointer object 這種本身為 top-level,稱 point to const 這種為 lower-level。
    int i = 0;
    int *const p1 = &i;       // we can't change the value of p1; const is top-level
    const int ci = 42;        // we cannot change ci; const is top-level
    const int *p2 = &ci;      // we can change p2; const is low-level
    const int *const p3 = p2; // right-most const is top-level, left-most is not
    const int &r = ci;        // const in reference types is always low-level

constexpr ( constant expression )

    constant expression 等於 expression 不能被改變,也就等於這個 expression 必須在編譯時期就可以被計算出來。以下為 constant expression 的例子。
    const int max_files = 20;        // max_files is a constant expression
    const int limit = max_files + 1; // limit is a constant expression
    int staff_size = 27;             // staff_size is not a constant expression
    const int sz = get_size();       // sz is not a constant expression
    int i; 
    const int size1 = i; 
    constexpr int size2 = i;      // error: the value of ‘i’ is not usable in a constant expression
    constexpr int mf = 20;        // 20 is a constant expression
    constexpr int limit = mf + 1; // mf + 1 is a constant expression
    constexpr int sz = size();    // ok only if size is a constexpr function
    constexpr 跟 pointer 的作用
    const int *p = nullptr;       // p is a pointer to a const int
    constexpr int *q = nullptr;   // q is a const pointer to int
相關文章 :
上一篇 :
下一篇 :

0 意見:

張貼留言

Popular Posts