广州网站建设公司怎么选,南宁优化网站网络服务,广州做淘宝的化妆品网站,厦门市建设局综合业务平台网站文章目录 const指针和函数声明const修饰指针const修饰函数const修饰容器const应用在函数中 const限定成员函数避免const重载的代码重复总结 const指针和函数声明 
const修饰指针 
char greeting[]  Hello;
char* p  greeting; 	// non-const 指针,// non-const 数据… 文章目录 const指针和函数声明const修饰指针const修饰函数const修饰容器const应用在函数中 const限定成员函数避免const重载的代码重复总结 const指针和函数声明 
const修饰指针 
char greeting[]  Hello;
char* p  greeting; 	// non-const 指针,// non-const 数据
const char* p  greeting; 	// non-const 指针,// const 数据
char* const p  greeting; // const 指针,// non-const 数据
const char* const p  greeting; // const 指针,// const 数据如果const出现在左侧指向的是常量const出现在右侧指针本身是常量。 
const修饰函数 
void f1(const Widget* pw); // f1接受一个指向常量Widget对象的指针
void f2(Widget const* pw); // f2也一样const出现在类型的左边或右边是等效的。 
const修饰容器 
std::vectorint vec;
//...// iter 的行为像 T* const
const std::vectorint::iterator iter  vec.begin();
*iter  10; 	// 可以修改指向的数据
iter; 	// 错误! iter 本身是 const
//cIter 的行为像 const T*
std::vectorint::const_iterator cIter vec.begin();
*cIter  10; 	// 错误! *cIter 是 const
cIter; 	// 正确, 可以修改迭代器本身const应用在函数中 令函数返回一个常量值往往可以降低因客户错误而造成的意外下面是一个有理数的例子。 
class Rational {  }; // 有理数! 
const Rational operator*(const Rational lhs, const Rational rhs);如果不小心把写成了 
Rational a, b, c;
(a * b)  c; 	// 对a*b的结果调用operator
if (a* b  c)  // 糟糕, 写错了应该是 但这时候由于我们返回的是一个const类型的所以该做法在编译的时候就会提示错误这也能让我们更快的找到错误。 
const限定成员函数 
在成员函数上使用const 1、使类的接口意图更明确。知道哪些函数可以修改一个对象哪些不能这很重要。 2、使得使用const对象成为可能。  C的一个重要特性仅在常量上不同的成员函数可以被重载。考虑表示文本块的类: 
class TextBlock {
public:TextBlock(std::string str) {text  str;}const char operator[](std::size_t position) const // const对象的operator[]{return text[position];} char operator[](std::size_t position) // non-const对象的operator[]{return text[position];} 
private:std::string text;
};void print(const TextBlock ctb) // 在这个函数中ctb是const
{std::cout  ctb[0]; // 调用const的TextBlock::operator[]
}//TextBlock的运算符[]可以这样使用:
TextBlock tb(Hello);std::cout  tb[0]; // 调用non-const的TextBlock::operator[]const TextBlock ctb(World);
std::cout  ctb[0]; // 调用const的TextBlock::operator[]print(tb);   // 调用const的TextBlock::operator[]
print(ctb);  // 调用const的TextBlock::operator[]tb[0]  x; //正确
ctb[0]  x; //错误避免const重载的代码重复 假设TextBlock中的[]运算符不仅返回对适当字符的引用还执行边界检查记录访问信息甚至可能进行数据完整性验证。 
class TextBlock {
public:...const char operator[](std::size_t position) const{... // do bounds checking... // log access data... // verify data integrityreturn text[position];}char operator[](std::size_t position) {... // do bounds checking... // log access data... // verify data integrityreturn text[position];}
private:std::string text;
};这边我们定义了一个const版本和一个非const版本的函数。 替代方案让operator[]的一个版本调用另一个版本。先写const版本然后脱离const限制。 
class TextBlock {
public:...const char operator[](std::size_t position) const{// 和前面一样.........return text[position];}char operator[](std::size_t position){//只需要调用const版本return const_castchar( //对op[]的返回类型抛弃const;//给*this的类型添加const;调用op的const版本[]static_castconst TextBlock(*this)[position] );}...
};1、在这种情况下去掉返回值上的const是安全的因为调用非const操作符[]的人首先必须有一个非const对象。 2、让非const操作符[]调用const版本是避免代码重复的安全方法。 
总结 
声明const可以借助编译器检测使用错误。const可以应用于任何作用域的对象、函数参数和返回类型以及作为一个整体的成员函数。编译器强制执行位常量但你应该使用逻辑常量进行编程。当const和非const成员函数具有本质上相同的实现时可以通过让非const版本调用const版本来避免代码重复。