2011年10月3日 星期一

Constant Pointer


[18.5] What's the difference between "Fred const* p", "Fred* const p" and "Fred const* const p"?

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.5

You have to read pointer declarations right-to-left.

  • Fred const* p means "p points to a constant Fred": the Fred object can't be changed via p.
  • Fred* const p means "p is a const pointer to a Fred": you can't change the pointer p, but you can change the Fred object via p.
  • Fred const* const p means "p is a constant pointer to a constant Fred": you can't change the pointer p itself, nor can you change the Fred object via p.

const X& x and X const& x are equivalent.
const X* x and X const* x are equivalent.

TypeX const ... 可寫成 const TypeX ....

沒有留言:

張貼留言