跳到主要內容

Pointers 指標

對於 const 修飾詞

1. Pointers to const [point to const object of pointers] (指向 const 物件的指標)
    // ptr is nonconst pointer and ptr point to const object of double type.
    const double *ptr;
   
    ptr 無法知道它所指向的物件是否真的為 const,所以它將它所指向的物件一律視為 const。
   
    這裡有一段話,pointers to const 所指物件並未保證一定不被改變
    事實上我們可以透過 const pointer 賦予給 nonconst pointer,不過若指向的物件是 const
    則一定不會被改變。


2. Const pointers [const of pointers] (本身為 const 的指標)
    // ptr is const pointer and ptr point to nonconst object of double type.
    double const *ptr;  // 等同 double *const ptr;
   
    如同其它 const 變數一樣,在建立 const pointer 時就該把它初始化。

留言