2012年12月13日 星期四

const, *, & 位置不同 代表意義也不同

CPolicyEnforcementModule * const p1 : p是一個const pointer 指向  CPolicyEnforcementModule (用途, array的p++, p會自動加array element size 的大小, 這裡會compile不過)

CPolicyEnforcementModule const * p2:  p是一個pinter 指向 const 的object. 要用P來呼叫 CPolicyEnforcementModule 裡面的 member function只能呼叫const的那種, (即使是get也不能呼叫, 除非在prototype和define 上都有定義成const

const CPolicyEnforcementModule *: 同p2

const * CPolicyEnforcementModule p: 沒有這個寫法, * 要在 Datatype 後

CPolicyEnforcementModule & const: (TBD….)

CPolicyEnforcementModule const &

const & CPolicyEnforcementModule

const CPolicyEnforcementModule &

 

Testing Program

const int constValue=3;
int notConstValue=3;

const int * p1 =&constValue; //a pointer point to a const integer   
//(*p1)++;//This is invalid. Through p to change the value it point is illegal
p1++; //this is ok


//int * const p2=&constValue; //error, the date can not change.
int * const p2=&notConstValue; //a const pointer point to a integer

(*p2)++;//this is ok, only the pointer is const, but the value can be changed
//p2++; //this is invalid, since it is a const pointer,


int const * p3=&constValue; //Same as p1   
//(*p3)++;
p3++;

//const * int p4=3; invalid decoration

沒有留言:

張貼留言