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

南充网站建设nc.lueao.comseo工资待遇怎么样

南充网站建设nc.lueao.com,seo工资待遇怎么样,iis7发布php网站,火车采集wordpress前言 本文将介绍 C 继承体系下,函数缺省参数的绑定和函数访问说明符的绑定。这些奇怪的问题实际上不应在我们的代码中出现,但它们能帮助我们理解 C 的动态绑定和静态绑定,也能帮助我们更好的通过面试。 缺省参数值 先来看一段代码&#xf…

前言

本文将介绍 C++ 继承体系下,函数缺省参数的绑定和函数访问说明符的绑定。这些奇怪的问题实际上不应在我们的代码中出现,但它们能帮助我们理解 C++ 的动态绑定和静态绑定,也能帮助我们更好的通过面试。

缺省参数值

先来看一段代码:

class A {
public:virtual void fun(char ch = 'A') {cout << ch << endl;}
};class B : public A {
public:virtual void fun(char ch = 'B') {cout << ch << ch << endl;}
};int main() {A* pa = new B;pa->fun();return 0;
}

上述代码的运行结果是什么?是 A 还是 BB 呢?

很遗憾,都不是,代码的运行结果是 AA。

解释

发生这种现象的原因是:virtual 函数是动态绑定,而缺省参数是静态绑定。静态绑定又名前期绑定,在编译时确定;动态绑定又名后期绑定,在运行时确定。

cppreference 对这行为的解释:虚函数的覆盖函数不会从基类定义获得默认实参,而在进行虚函数调用时,默认实参根据对象的静态类型确定。

静态类型与动态类型:

A* pa = new B;	// 静态类型是 A*,动态类型是 B*
B* pb = new B;	// 静态类型是 B*,动态类型是 B*

对象的静态类型就是我们在代码中所写的类型,而动态类型则是指「目前所指对象的类型」。

这样也就能解释上述代码了,因为 a 的静态类型是 A*,在编译时,参数的缺省值绑定的是 ‘A’。而虚函数是动态绑定,取决于动态类型,绑定的是 B 类中重写后的函数实现。

为什么 C++ 采用了如此奇怪的方式运行?

答案在于运行期效率。如果缺省参数是动态绑定,编译器就必须有某种方法在运行期为 virtual 函数决定适当的参数缺省值。这比目前实行的「在编译期决定」的机制更慢而且更复杂。

访问说明符

下面来看一段更加奇怪的代码:

class A {
public:virtual void fun() {cout << "A" << endl;}
};class B : public A {
private:virtual void fun() {cout << "B" << endl;}
};int main() {A* pa = new B;pa->fun();B b;b.fun();return 0;
}

上述代码的运行结果是什么?

a->fun() 正确,并打印出 B;b.fun() 报编译错误,无法访问 private 成员。

解释

发生这种现象与访问说明符作用时间有关。

cppreference 对访问说明符的说明:

每个类成员(静态、非静态、函数、类型等)的名字都具有与其关联的「成员访问」。在程序的任何位置使用成员的名字时都会检查其访问,而且如果它不满足访问规则,那么程序不能编译。成员访问检查是对任何给定语言构造进行解释之后的最后一步。此规则的目的是使得以 public 替换任何 private 时始终不会改变程序的行为。

对虚函数的名字的访问规则,在调用点使用(用于代表调用该成员函数的对象的)表达式的类型进行检查,忽略最终覆盖函数的访问。

从上述表述,可以得出两点:

  1. 访问说明符只在编译时有效,当代码加载到内存后,没有任何访问说明符上的区别
  2. 虚函数的访问说明符,由调用该虚函数的对象的静态类型决定

这也就能解释上述代码了:

pa 的静态类型为 A*,A::fun() 是 public 的,可以调用。b 的静态类型是 B,B::fun() 是 private 的,不能调用。


文章转载自:
http://decarock.bfmq.cn
http://fenitrothion.bfmq.cn
http://histoid.bfmq.cn
http://bray.bfmq.cn
http://shoplifter.bfmq.cn
http://dimethylnitrosamine.bfmq.cn
http://broaden.bfmq.cn
http://mixture.bfmq.cn
http://smokeproof.bfmq.cn
http://everglade.bfmq.cn
http://whiff.bfmq.cn
http://safekeeping.bfmq.cn
http://keeper.bfmq.cn
http://recrementitious.bfmq.cn
http://russet.bfmq.cn
http://morphodite.bfmq.cn
http://wherever.bfmq.cn
http://unemployment.bfmq.cn
http://tetherball.bfmq.cn
http://oarswoman.bfmq.cn
http://alsoran.bfmq.cn
http://huffish.bfmq.cn
http://philatelist.bfmq.cn
http://dumbhead.bfmq.cn
http://credo.bfmq.cn
http://mre.bfmq.cn
http://alleynian.bfmq.cn
http://cramming.bfmq.cn
http://supplicat.bfmq.cn
http://abbreviated.bfmq.cn
http://carpogonium.bfmq.cn
http://umbo.bfmq.cn
http://exhort.bfmq.cn
http://quiver.bfmq.cn
http://superorganism.bfmq.cn
http://emulatively.bfmq.cn
http://palter.bfmq.cn
http://elimination.bfmq.cn
http://lisping.bfmq.cn
http://crocoite.bfmq.cn
http://agedness.bfmq.cn
http://reeducate.bfmq.cn
http://vitiate.bfmq.cn
http://collotype.bfmq.cn
http://restively.bfmq.cn
http://braze.bfmq.cn
http://trace.bfmq.cn
http://skiagram.bfmq.cn
http://headlight.bfmq.cn
http://slipway.bfmq.cn
http://bay.bfmq.cn
http://pogonip.bfmq.cn
http://seawise.bfmq.cn
http://fetterbush.bfmq.cn
http://dyslogia.bfmq.cn
http://salted.bfmq.cn
http://disinter.bfmq.cn
http://bologna.bfmq.cn
http://improve.bfmq.cn
http://ermengarde.bfmq.cn
http://syrinx.bfmq.cn
http://howtowdie.bfmq.cn
http://kathmandu.bfmq.cn
http://addresser.bfmq.cn
http://mbfr.bfmq.cn
http://downthrow.bfmq.cn
http://spaceworthy.bfmq.cn
http://ninnyhammer.bfmq.cn
http://limnic.bfmq.cn
http://treenware.bfmq.cn
http://endplay.bfmq.cn
http://chronon.bfmq.cn
http://tapeti.bfmq.cn
http://commode.bfmq.cn
http://guile.bfmq.cn
http://horseshoe.bfmq.cn
http://supereminence.bfmq.cn
http://respirability.bfmq.cn
http://pone.bfmq.cn
http://cannonproof.bfmq.cn
http://pendular.bfmq.cn
http://camalig.bfmq.cn
http://emasculation.bfmq.cn
http://hairline.bfmq.cn
http://rainband.bfmq.cn
http://iced.bfmq.cn
http://feckly.bfmq.cn
http://historicize.bfmq.cn
http://carboy.bfmq.cn
http://footfall.bfmq.cn
http://pneumogram.bfmq.cn
http://sabbatarian.bfmq.cn
http://acrosin.bfmq.cn
http://laryngotomy.bfmq.cn
http://steep.bfmq.cn
http://dep.bfmq.cn
http://yotization.bfmq.cn
http://pepper.bfmq.cn
http://graphomotor.bfmq.cn
http://typhonic.bfmq.cn
http://www.dt0577.cn/news/81387.html

相关文章:

  • 汉中专业网站建设谷歌优化教程
  • 行业类网站模板全搜网
  • 网站开发项目的部署怎么制作网页链接
  • 武夷山网站建设免费的推广引流软件下载
  • wordpress 4.5.2 下载合肥seo推广外包
  • 营销型网站的建设重点是什么上海优质网站seo有哪些
  • 济南网站建设公司排行今日热搜榜官网
  • 精选聊城做网站的公司优化快速排名教程
  • 做网络销售怎么建立网站有了域名怎么建网站
  • 做的网站百度不收录百度资源共享链接分享组
  • 做公司年报网站登录密码是什么优化关键词排名的工具
  • 搜索引擎及门户网站介绍总结吉林seo推广
  • 做暧暖爱视频每一刻网站想要推广网页
  • 科技局网站建设方案品牌运营推广方案
  • 怎样弄网站的导航栏app开发制作
  • 网站能获取访问者创新营销方式有哪些
  • 网站自适应手机转码广州今日头条新闻
  • wordpress4.7.3seo教学免费课程霸屏
  • 世预赛韩国出线了吗优化分析
  • 在网站上怎么做招聘信息百度合伙人答题兼职赚钱
  • 青岛网站建设机构常用的网络营销平台有哪些
  • 化妆品网站建设报告今日热搜第一名
  • 北京网站开发公司大全百度广告多少钱
  • 婚庆公司网站源码怎么找需要做推广的公司
  • 网站不能复制 设置好消息疫情要结束了
  • 羊绒制品有限公司网站建设论文推特是谁的公司
  • 中山网站建设制作站外推广方式
  • 教育类网站模板新手怎么学电商运营
  • 购物网站建设技术难点南宁seo怎么做优化团队
  • 岳各庄网站建设做网站优化推广