char greeting[] = “Hello”; char *p = greeting; //non-const pointer, non-const data const char *p = greeting; //non-const pointer, const data char *const p = greeting; //const pointer, non-const data const char *const p = greeting //const pointer, const data
std::vectorvec; const std::vector ::iterator iter = vec.begin(); //iter的作用像個T *const *iter = 10 //ok. 改變iter所指物 ++iter; //error. iter是const const std::vector ::const_iterator cIter = vec.begin(); //cIter的作用像個const T * *cIter = 10 //error. *cIter是const ++cIter; //error. 改變cIter
class Rational {…}; const Rational::operator *(const Rational *lhs, const Rational *rhs) const參數聲明為const,避免它修改