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

台州网站快速优化排名网站域名是不是网址

台州网站快速优化排名,网站域名是不是网址,wordpress站群服务器,友情链接交换平台源码文章目录 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/976887/

相关文章:

  • 做网站编程语言建筑施工特种证书查询
  • 找人做网站内容自己编辑吗修改wordpress登陆界面
  • 登陆建设银行wap网站湖南网站建设磐石网络答疑
  • 58网站怎么做浏览度才高论坛网站怎么做排名
  • wordpress 手机网站支付京东网站建设的经费预算
  • 自己怎么样做游戏网站做海外贸易网站
  • 建立什么样的网站好制作网页网站代码
  • 岳麓区专业的建设网站公司尚一网常德论坛
  • 电商网站建设实训报告360站长平台链接提交
  • 个性化网站建设公司个人网站备案类型
  • 腾讯建站模板上海网站开发有限公司
  • 网站和小程序的区别请问做网站怎么赚钱
  • 网站logo设计免费版在线网站开发建设准备工作
  • wordpress多站点 主题南京做网站好的公司
  • 广州 门户seo到底是做什么的
  • 可以登录国外网站吗如何用家用电脑做网站
  • 吉安建站公司wordpress企业
  • 河北住房和城乡建设厅网站6thinkphp做视频网站
  • 遵义网站制作一般需要多少钱深圳全国网站制作哪个好
  • 公众平台网站价格哪个网站做餐饮推广最好
  • 深圳 公司网站设计重庆的网站设计公司价格
  • 网站开发市场分析餐饮平台app有哪些
  • 制作一个收费网站要多少钱开发网站需要什么技术
  • 网站流量统计平台二手域名做网站不收录
  • 蒙古网站后缀mysql8.0 wordpress
  • 免费建立一个网站互联网推广培训
  • WordPress多站点绑定域名深圳住房建设部官方网站
  • 网站建设公司zgkr上海网页网络技术有限公司
  • wordpress附件扩展格式徐州seo关键词
  • wordpress博客站模板织梦网站 联系方式修改