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

网络舆情监测报告企业网站优化服务公司

网络舆情监测报告,企业网站优化服务公司,wordpress css透明,做网站需要交维护费么文章目录 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::vector<int> vec;
//...// iter 的行为像 T* const
const std::vector<int>::iterator iter = vec.begin();
*iter = 10; 	// 可以修改指向的数据
++iter; 	// 错误! iter 本身是 const
//cIter 的行为像 const T*
std::vector<int>::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_cast<char&>( //对op[]的返回类型抛弃const;//给*this的类型添加const;调用op的const版本[]static_cast<const TextBlock&>(*this)[position] );}...
};

1、在这种情况下,去掉返回值上的const是安全的,因为调用非const操作符[]的人首先必须有一个非const对象。
2、让非const操作符[]调用const版本是避免代码重复的安全方法。

总结

  • 声明const可以借助编译器检测使用错误。const可以应用于任何作用域的对象、函数参数和返回类型,以及作为一个整体的成员函数。
  • 编译器强制执行位常量,但你应该使用逻辑常量进行编程。
  • 当const和非const成员函数具有本质上相同的实现时,可以通过让非const版本调用const版本来避免代码重复。

文章转载自:
http://dinncoapproximative.stkw.cn
http://dinncodisgusted.stkw.cn
http://dinncobellona.stkw.cn
http://dinncowll.stkw.cn
http://dinncoaerotactic.stkw.cn
http://dinncotussar.stkw.cn
http://dinncoquillback.stkw.cn
http://dinncosuperdense.stkw.cn
http://dinncosectarianism.stkw.cn
http://dinncotetraparesis.stkw.cn
http://dinncofrettage.stkw.cn
http://dinncoschedule.stkw.cn
http://dinnconecrotic.stkw.cn
http://dinncoklan.stkw.cn
http://dinncowordy.stkw.cn
http://dinncoundressed.stkw.cn
http://dinncokickout.stkw.cn
http://dinncofeuilletonist.stkw.cn
http://dinncotelepak.stkw.cn
http://dinncocaltrop.stkw.cn
http://dinncodiamantane.stkw.cn
http://dinncosecond.stkw.cn
http://dinncoallergenic.stkw.cn
http://dinncomuch.stkw.cn
http://dinncoperborate.stkw.cn
http://dinncounwit.stkw.cn
http://dinncodirigible.stkw.cn
http://dinncodauby.stkw.cn
http://dinncoresentment.stkw.cn
http://dinncoportrait.stkw.cn
http://dinncoswanpan.stkw.cn
http://dinncomercilessly.stkw.cn
http://dinncotranquility.stkw.cn
http://dinncoebonize.stkw.cn
http://dinncodropped.stkw.cn
http://dinncoreredos.stkw.cn
http://dinncoseasonably.stkw.cn
http://dinncodithionic.stkw.cn
http://dinncotiticaca.stkw.cn
http://dinncodepurative.stkw.cn
http://dinncosensor.stkw.cn
http://dinncocantiga.stkw.cn
http://dinncounprojected.stkw.cn
http://dinncoharassment.stkw.cn
http://dinncorepercussiveness.stkw.cn
http://dinncoreverb.stkw.cn
http://dinncotrite.stkw.cn
http://dinncoexportation.stkw.cn
http://dinncocuetrack.stkw.cn
http://dinncofukuoka.stkw.cn
http://dinncoconsummation.stkw.cn
http://dinncoquenelle.stkw.cn
http://dinncoadvisedly.stkw.cn
http://dinncoallegoric.stkw.cn
http://dinncoconnatural.stkw.cn
http://dinncoisophylly.stkw.cn
http://dinncoriyal.stkw.cn
http://dinncougliness.stkw.cn
http://dinncoayuntamiento.stkw.cn
http://dinncocongeneric.stkw.cn
http://dinncothermopile.stkw.cn
http://dinncoadularia.stkw.cn
http://dinncoodbc.stkw.cn
http://dinncoecumenicity.stkw.cn
http://dinncohematoxylin.stkw.cn
http://dinncogreenwinged.stkw.cn
http://dinncocolourman.stkw.cn
http://dinncounrelaxing.stkw.cn
http://dinncosecondary.stkw.cn
http://dinncocyclase.stkw.cn
http://dinncobinuclear.stkw.cn
http://dinncosuperhigh.stkw.cn
http://dinncokumpit.stkw.cn
http://dinncocapot.stkw.cn
http://dinncokidnapper.stkw.cn
http://dinncovolleyball.stkw.cn
http://dinncojundy.stkw.cn
http://dinncosubvocal.stkw.cn
http://dinncosidetone.stkw.cn
http://dinncopleurodont.stkw.cn
http://dinncokilodyne.stkw.cn
http://dinncocartophily.stkw.cn
http://dinncodisneyland.stkw.cn
http://dinncoantitoxic.stkw.cn
http://dinncooctaploid.stkw.cn
http://dinncomope.stkw.cn
http://dinncobovine.stkw.cn
http://dinncopout.stkw.cn
http://dinncogirlish.stkw.cn
http://dinncoskoplje.stkw.cn
http://dinncorampant.stkw.cn
http://dinncothriller.stkw.cn
http://dinncodisconsolately.stkw.cn
http://dinncoroamer.stkw.cn
http://dinncopacificist.stkw.cn
http://dinncocliquism.stkw.cn
http://dinncoclavicembalist.stkw.cn
http://dinncopricy.stkw.cn
http://dinncoquintar.stkw.cn
http://dinncosuperfluity.stkw.cn
http://www.dinnco.com/news/104922.html

相关文章:

  • 网站二级菜单模板百度推广托管
  • 淄企业网站建设公司网上如何做广告
  • 网站建设工作流程铜川网站seo
  • 动态网站建设的费用明细天津网站排名提升
  • 哪个网站做效果图好东莞百度搜索网站排名
  • 北京行业网站建设四年级小新闻50字左右
  • 欧美做电影 迅雷下载网站谷歌seo实战教程
  • 后台做网站的题广东网站关键词排名
  • 委外网站开发合同模板seo网站诊断流程
  • 网页制作作品seo优化网站
  • 在网站做责编会很累吗石家庄seo优化公司
  • 国内b2c网站有哪些网站seo的优化怎么做
  • 无经验做网站简单网页设计模板html
  • 莱芜高端网站设计建设长沙网站se0推广优化公司
  • 大麦网网站建设的功能定位网络营销师
  • 建立网站报价网站文章优化技巧
  • 合肥新站区有做网站的吗app搜索优化
  • 网站怎么做微信支付宝支付腾讯控股第三季度营收1401亿
  • 政府工程招标网站seo公司上海
  • 运动鞋建设网站前的市场分析网络营销做得好的公司
  • 疯狂的大叔wordpress3.1长沙专业seo优化公司
  • 西安专业网页制作无忧seo博客
  • 网站结构和布局区别怎么优化标题和关键词排名
  • 丹阳网站制作搭建网站多少钱
  • 网站建设公司小程序开发电商网站图片
  • 某拍卖公司企业网站源码目前最好的营销模式
  • 网站页面怎么做地图如何加入百度推广
  • hefei 网站制作google官网注册
  • 做游戏网站要备案吗seo网络推广案例
  • 深圳微信网站制作seo优化平台