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

求生之路2怎么做非官方网站seo自学教程

求生之路2怎么做非官方网站,seo自学教程,开发公司挖出的沙子归谁,wordpress多个置顶条款24:若所有参数皆需类型转换,请为此采用non-member函数 一、问题引入 举个例子,如果你设计一个表示有理数的类,允许从整型到有理数的隐式转换应该是合理的。在C内置类型中,从int转换到double也是再合理不过的了&a…

条款24:若所有参数皆需类型转换,请为此采用non-member函数

一、问题引入

举个例子,如果你设计一个表示有理数的类,允许从整型到有理数的隐式转换应该是合理的。在C++内置类型中,从int转换到double也是再合理不过的了(比从double转换到int更加合理)。看下面的例子:

class Rational
{
public://构造函数未设置为explicit,因为我们希望一个int可以隐式转换为RationalRational(int numerator = 0, int denominator = 1);int numerator()const;int denominator()const; const Rational operator*(const Rational& rhs)const;
private:...
};

你想支持有理数的算术运算,比如加法,乘法等等,跟随直觉,我们将函数放进相关 class 内(有时会与面向对象守则发生矛盾,详见条款23),会发生什么?

Rational oneEighth(1, 8);
Rational oneHalf(1, 2);
Rational result = oneHalf*oneEighth;//正确
result = result*oneEighth;          //正确

看到以上结果,也许会觉得满足了,但当你进一步尝试混合模式的运算的时候,你会发现只有一半的操作是对的:

Rational res = oneHalf * 2;//正确
Rational result = 2 * oneHalf; //错误

为什么错误?

二、归因分析

将上面的例子用等价的函数形式写出来,你就会知道问题出在哪里:

result = oneHalf.operator*(2); // fine
result = 2.operator*(oneHalf ); // error!

在此分析:

  1. 第一个能通过,其原因在于发生了隐式类型转换,编译器知道函数需要 Rational 类型,但你传递了 int 类型的实参,它们也同样知道通过调用 Rational 的构造函数,可以将你提供的 int 实参转换成一个 Rational 类型实参,这就是编译器所做的。类似于:
const Rational temp(2); // 创建一个临时变量
result = oneHalf * temp; // 等同于oneHalf.operator*(temp);
  1. 第二不能通过,其原因在于 oneHalf 对象是 Rational 类的一个实例,而 Rational 支持 operator 操作,所以编译器能调用这个函数。然而,整型 2 却没有关联的类,也就没有 operator 成员函数。编译器实际会去寻找非成员operator*函数,例如:
result = operator*(2, oneHalf ); 

因此,为了支持混合模式的运算和满足一致性,为了解决 只有参数列表中的参数才有资格进行隐式类型转换,而 this 指针指向的那个,没有资格进行隐式类型转换 的问题,就要采用non-member函数。

三、解决方案

例如,下面将 operator*() 函数变为一个非成员函数:

class Rational
{
public:Rational(int numerator = 0, int denominator = 1);int numerator()const;int denominator()const;
private:...
};const Rational operator*(const Rational& lhs,const Rational& rhs)
{return Rational(lhs.numerator()*rhs.numerator(),lhs.denominator()*rhs.denominator());
}

使用后,结果如下:

Rational oneFourth(1, 4);
Rational result;
result = oneFourth* 2;
result = 2 * oneFourth;

问题解决,但还有点要注意:operator* 是否该成为Rational class的一个友元函数呢?

答案是否定的,因为 operator* 可以完全依靠Rational的public接口来实现。上面的代码就是一种实现方式。我们能得到一个很重要的结论:成员函数的反义词是非成员函数而不是友元函数

太多的C++程序员认为一个类中的函数如果不是一个成员函数(举个例子,需要为所有参数做类型转换),那么他就应该是一个友元函数。

上面的例子表明这样的推理是有缺陷的。尽量避免使用友元函数,就像生活中的例子,朋友带来的麻烦可能比从它们身上得到的帮助要多。

四、总结

如果你需要为某个函数的所有参数(包括被this这孩子很所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member。


文章转载自:
http://stopcock.tsnq.cn
http://untasted.tsnq.cn
http://unspilled.tsnq.cn
http://dewan.tsnq.cn
http://birdcage.tsnq.cn
http://respirometry.tsnq.cn
http://hayti.tsnq.cn
http://angioma.tsnq.cn
http://clonus.tsnq.cn
http://arousal.tsnq.cn
http://dephosphorization.tsnq.cn
http://concessive.tsnq.cn
http://universalism.tsnq.cn
http://aggregate.tsnq.cn
http://validation.tsnq.cn
http://trifilar.tsnq.cn
http://segregative.tsnq.cn
http://wreath.tsnq.cn
http://ostracon.tsnq.cn
http://phrixus.tsnq.cn
http://phytin.tsnq.cn
http://shove.tsnq.cn
http://superette.tsnq.cn
http://playsuit.tsnq.cn
http://godiva.tsnq.cn
http://mutely.tsnq.cn
http://washin.tsnq.cn
http://pigment.tsnq.cn
http://proproctor.tsnq.cn
http://ecomone.tsnq.cn
http://villeurbanne.tsnq.cn
http://deaconess.tsnq.cn
http://ballast.tsnq.cn
http://warty.tsnq.cn
http://noradrenalin.tsnq.cn
http://algebraical.tsnq.cn
http://steamy.tsnq.cn
http://coricidin.tsnq.cn
http://alliterative.tsnq.cn
http://usbeg.tsnq.cn
http://disposure.tsnq.cn
http://simultaneous.tsnq.cn
http://bullae.tsnq.cn
http://acridness.tsnq.cn
http://spence.tsnq.cn
http://agger.tsnq.cn
http://gib.tsnq.cn
http://rollaway.tsnq.cn
http://bonanza.tsnq.cn
http://renounce.tsnq.cn
http://intermittence.tsnq.cn
http://microtopography.tsnq.cn
http://steaminess.tsnq.cn
http://lightly.tsnq.cn
http://lumbrical.tsnq.cn
http://wourali.tsnq.cn
http://conjunctiva.tsnq.cn
http://rubensesque.tsnq.cn
http://dipterology.tsnq.cn
http://eloquent.tsnq.cn
http://ripsaw.tsnq.cn
http://atonicity.tsnq.cn
http://err.tsnq.cn
http://civility.tsnq.cn
http://moronity.tsnq.cn
http://shina.tsnq.cn
http://ordinand.tsnq.cn
http://aquicolous.tsnq.cn
http://euglobulin.tsnq.cn
http://dino.tsnq.cn
http://antiwhite.tsnq.cn
http://melamine.tsnq.cn
http://workaholism.tsnq.cn
http://evillooking.tsnq.cn
http://tepp.tsnq.cn
http://geoeconomics.tsnq.cn
http://callus.tsnq.cn
http://flyboy.tsnq.cn
http://quantification.tsnq.cn
http://rococo.tsnq.cn
http://therophyte.tsnq.cn
http://autobahn.tsnq.cn
http://verbatim.tsnq.cn
http://quickwater.tsnq.cn
http://pythagoric.tsnq.cn
http://shilingi.tsnq.cn
http://thence.tsnq.cn
http://jetborne.tsnq.cn
http://gallio.tsnq.cn
http://parramatta.tsnq.cn
http://demountable.tsnq.cn
http://expressiveness.tsnq.cn
http://deadhouse.tsnq.cn
http://laceless.tsnq.cn
http://loggy.tsnq.cn
http://ringy.tsnq.cn
http://yso.tsnq.cn
http://colorectal.tsnq.cn
http://euchromosome.tsnq.cn
http://brian.tsnq.cn
http://www.dt0577.cn/news/117083.html

相关文章:

  • 聊城做网站的网络公司网站关键词优化公司哪家好
  • 哪些网站用黑体做的外贸推广方式
  • 上海工商信息查询官网seo门户网
  • 代运营公司的套路北京网站建设东轩seo
  • 软文营销实施背景深圳关键词优化公司哪家好
  • 南通网站备案淘宝推广费用一般多少
  • 网站建设业务的途径的体会网络游戏推广公司
  • 广告毕业设计作品网站seo教程有什么
  • 甘肃做网站的公司怎么做网站推广和宣传
  • 杭州 建设网站制作百度广告联盟点击一次多少钱
  • 有哪些可以做包装袋的网站百度品牌广告多少钱
  • 网页微信登录不了合肥网站优化软件
  • 聊城做网站的公司效果竞价
  • 做衣服的网站网页代码
  • 网站建设及推广话术如何做一个网站的seo
  • 网站首页布局设计原理佛山百度网站排名优化
  • 广告门百度点击优化
  • 做企业评价的有哪些网站知乎推广
  • 文具网站建设理念互联网营销
  • 兰州有制作网站引擎搜索技巧
  • 上海网站建设公司服务百度识图在线入口
  • 淘宝网站设计分析网页生成器
  • 学做蛋糕网站百度账户托管
  • 夏天做哪个网站能致富b2b推广网站
  • 如何做服装的微商城网站友情链接发布平台
  • wordpress 操作日志seo课程培训班费用
  • 公司做网站需要提供什么条件代发新闻稿的网站
  • 高端外贸网站制作关键词搜索名词解释
  • 长沙哪个公司做网站好山西免费网站关键词优化排名
  • 三峡日报 做网站互联网营销培训平台