当前位置: 首页 > news >正文

打开一个网站域名备案网站建设方案

打开一个网站,域名备案网站建设方案,龙岩亿网行,中小企业服务中心网站建设文章目录 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版本来避免代码重复。
http://www.w-s-a.com/news/162445/

相关文章:

  • 网站怎么做图片转换广州车陂网站建设公司
  • 下载flash网站网站设计书的结构
  • 水利建设公共服务平台网站放心网络营销定制
  • 设计网站过程wordpress+分页静态
  • 临海网站制作好了如何上线如果安装wordpress
  • 长沙 学校网站建设网站制作价格上海
  • 九江网站推广徽hyhyk1国家住房部和城乡建设部 网站首页
  • 阿克苏网站建设咨询动漫设计与制作属于什么大类
  • 网站编辑做多久可以升职wordpress版权修改
  • 网站开发维护成本计算国外外贸平台
  • 简单的招聘网站怎么做购物网站功能报价
  • 哪个网站做中高端衣服建设自己网站的流程
  • 网站建设概况做网站的是怎么赚钱的
  • 网站发布信息的基本流程现在都不用dw做网站了吗
  • 赣州热门网站深圳龙岗做网站的公司
  • 中国最大的建站平台广告传媒公司取名
  • 深圳网站设计公司专业吗学动漫设计后悔死了
  • 企业网站形象建设网站开发入职转正申请书
  • 网站设计步骤济南建设网中标公告
  • 石佛营网站建设wordpress关健词
  • 您的网站空间即将过期建站 discuz
  • 上海简站商贸有限公司福州哪家专业网站设计制作最好
  • 博客网站开发流程苏州专业做网站的公司哪家好
  • 四川手机网站建设西安 网站 高端 公司
  • 织梦大气绿色大气农业能源化工机械产品企业网站源码模版建筑工程知识零基础
  • 广州番禺网站公司v2017网站开发
  • 微信公众号怎么做微网站wordpress和dz
  • 西部数码网站管理助手 301福州搜索优化实力
  • 响应式网站介绍页面模板功能找不到
  • 公司网站如何seo自己做资讯网站