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

南京网站开发南京乐识不错武汉最新疫情

南京网站开发南京乐识不错,武汉最新疫情,视频直播系统开发,wordpress tab缩进支持重载操作符是c的一个特性,先不管好不好用,这起码能让它看起来比其他语言NB很多,但真正了解重载操作符后,就会发现这个特性...就这?本文分两个部分 重载操作符简介和使用——适用新手重载操作符的原理和sao操作——…

支持重载操作符是c++的一个特性,先不管好不好用,这起码能让它看起来比其他语言NB很多,但真正了解重载操作符后,就会发现这个特性...就这?本文分两个部分

  • 重载操作符简介和使用——适用新手
  • 重载操作符的原理和sao操作——适用装杯选手

1 简介 & 使用

c++支持重载的操作符有

算术操作符

+、-、*、/、%

关系操作符

==、!=、>、<、>=、<=

逻辑操作符

!、&&、||

位操作符

~、&、|、^、<<、>>

赋值操作符

=、+=、-=、*=、/=、%=、&=、|=、^=、>>=、<<=

下标操作符

[]

函数调用操作符

()

成员访问操作符

->

指针操作符

*(解引用)、&(取地址)

逗号操作符

,

这些操作符按照操作数个数不同分为单目操作符、双目操作符、多目操作符

单目操作符

!、~、->、*(解引用)、&(取地址)

双目操作符

其他

多目操作符

()

为什么要按这个分类?别急,继续看...

用法

重载操作符实际上就是重新定义操作符的行为函数,不过这里需要用到一个关键字operator。大多数操作符有两种重载方式

  • 类成员函数重载
  • 全局函数重载

例如

// 成员函数重载Point类的"+"操作符和"~"操作符 —— 定义在类内部
struct Point {Point operator+(const Point& oth) const { ... }Point operator~() const { ... }
};// 全局函数重载Point类的"+"操作符和"~"操作符 —— 定义在类外部
Point operator+(const Point& self, const Point& oth) { ... }
Point operator~(const Point& self) { ... }

一些限制

有的程序员不喜欢使用重载操作符,因为它的特性实在难以琢磨。但实际上它只是在定义函数的基础上又一些限制

  • 基本数据类型:只能通过全局函数重载操作符。因为基本数据类型是语言内置的,无法自定义。
  • 自定义类型(这也是最通用的用法)
    • 所有支持重载的操作符都可以通过类成员函数重载
    • 大部分支持重载的操作符都可以通过全局函数重载,有3个例外: ()[]->
  • 每个操作符重载时形参个数都是固定的,按照单目、双目、多目分类,形参个数要求如下

单目操作符

双目操作符

多目操作符

全局函数

1

2

-

类成员函数

0

1

0个或多个

以上内容,足够正确使用重载操作符。但有些同学会觉得很难理解和正确使用,下面一起理解一下重载操作符的本质,理解本质之后再回头来看就会发现重载操作符原来...就这?

2 重载操作符的本质

重载操作符本质上是特殊的函数。在c++中,函数具有以下形式

返回值 函数名 (形参列表) { 函数体 }

可以看到,函数由四个部分组成:返回值、函数名、形参列表、函数体。重载操作符本质上也是函数,只是在 函数名形参列表 两部分具有特殊性,另外 调用方式 也很特殊。

函数名

重载操作符的函数名是由operator关键字和操作符符号组成的,例如 operator+、operator!等等。

形参列表

形参列表的参数数量是固定的,具体见下面的表格

单目操作符

双目操作符

操作符-特殊

全局函数

1 (self)

2 (self, 任意类型)

-

类成员函数

0

1 (任意类型)

0个或多个

一个没用的小知识

大家可能已经发现了,对于同一个操作符而言全局函数重载总是比类成员函数重载多一个参数,这个多出来的参数有两个特点:

  • 一定是全局函数的第一个形参。
  • 类型一定是重载操作符的目标类型。例如

成员函数重载: Point operator+(const Point& oth) const { ... }

全局函数重载: Point operator+(const Point& self, const Point& oth) { ... }

这里隐藏了一个成员函数的秘密:在c++中,成员函数默认第一个参数是this指针,只不过写法上忽略了。大家感兴趣的话可以研究下成员函数的汇编码,其中的奥秘就一目了然了。熟悉python语法的同学应该能很容易理解,python的类成员函数必须把第一个参数写成self,这样才能在函数体内访问成员变量。

调用方式

操作符的使用和函数调用有直观上的差别,如何使用操作符大家应该都很熟悉,这里就不举例子了。值得一提的是操作符的使用本质上还是函数调用。举个例子,用全局函数重载"+"操作符

struct Point {Point(int x, int y) : x(x), y(y) { }int x;int y;
};// 重载Point的“+”操作符
Point operator+(const Point& self, const Point& oth) { return Point(self.x + oth.x, self.y + oth.y); }int main() {Point p1(20, 60);Point p2(2, 5);Point pAdd = p1 + p2;    // 使用Point的“+”操作符
}

上面这段代码的汇编代码如下(为了方便大家能抓住重点,对汇编码做了精简)

Point::Point(int, int) [base object constructor]:...ret
operator+(Point const&, Point const&):...ret
main:...mov     rsi, rdxmov     rdi, raxcall    operator+(Point const&, Point const&)...ret

可以看到,编译器把Point pAdd = p1 + p2;这句c++代码编译成了call operator+(Point const&, Point const&),也就是调用函数operator+(Point const&, Point const&)

看到这里,大家脑子里会不会闪过一个大胆的想法——在c++代码中直接调用函数operator+(Point const&, Point const&)会怎么样?就像...

...Point pAdd = operator+(p1, p2);    // Point pAdd = p1 + p2;
...

然后我们会发现代码竟然可以 正!常!运!行!而且对应的汇编代码也一!模!一!样!所以,操作符也可以通过函数掉调用的方式使用

前面研究的是全局函数重载的行为,那么成员函数重载呢?我们一起来看看

struct Point {Point(int x, int y) : x(x), y(y) {}// 重载Point的“+”操作符Point operator+(const Point& oth) const { return Point(x + oth.x, y + oth.y); }int x;int y;
};int main() {Point p1(20, 60);Point p2(2, 5);Point pAdd = p1 + p2;    // 使用Point的“+”操作符
}

对应的汇编代码

Point::Point(int, int) [base object constructor]:...ret
Point::operator+(Point const&) const:...ret
main:...mov     rsi, rdxmov     rdi, raxcall    Point::operator+(Point const&) const...ret

编译器把Point pAdd = p1 + p2;这句c++代码编译成了call Point::operator+(Point const&) const。不难发现,全局函数重载和成员函数重载对应的汇编代码的operator+符号不一样:

  • 形参列表不同。全局函数有两个形参,成员函数有一个形参。
  • 域前缀不同。全局函数没有域前缀,类成员函数汇编后携带了域信息Point

成员函数重载操作符也可以像函数调用一样使用,只不过需要遵守成员函数的调用规则

...Point pAdd = p1.operator+(p2);    // Point pAdd = p1 + p2;
...

下面做个简单对比

全局函数重载

类成员函数重载

c++代码

Point operator+(const Point& self, const Point& oth) { ... }
struct Point {Point operator+(const Point& oth) const { ... }
};

汇编代码

operator+(Point const&, Point const&)

Point::operator+(Point const&) const

函数式调用

Point pAdd = operator+(p1, p2);

Point pAdd = p1.operator+(p2);

以上就是重载操作符相对于普通函数的特殊所在,除了这几个特殊点其他方面没有任何不同。在使用的时候我们可以对返回值形参类型函数体为!所!欲!为!

严重警告

  • 尽管规则允许我们直接调用重载操作符,但尽量不要这样做,因为这样做的话c++的NB程度会锐减,而且一定会有人在看代码的时候骂你。
  • 尽管规则上可以为所欲为,还是建议各位保持冷静,玩玩儿可以千万不要在项目中写出太超越道德上限的代码。像这样
struct Cat { ... };
struct Dog { ... };Dog operator+(const Cat& self, const Cat& oth) {...return Dog();
}

一只猫加另一只猫,得到一条狗?


文章转载自:
http://empocket.fwrr.cn
http://prizewinner.fwrr.cn
http://homogony.fwrr.cn
http://crinoid.fwrr.cn
http://batwing.fwrr.cn
http://astigmatical.fwrr.cn
http://widthwise.fwrr.cn
http://dude.fwrr.cn
http://clifty.fwrr.cn
http://moonwatcher.fwrr.cn
http://mobbish.fwrr.cn
http://predawn.fwrr.cn
http://meandrous.fwrr.cn
http://esr.fwrr.cn
http://speckless.fwrr.cn
http://lang.fwrr.cn
http://manado.fwrr.cn
http://violator.fwrr.cn
http://puzzledom.fwrr.cn
http://liverwurst.fwrr.cn
http://saturniid.fwrr.cn
http://store.fwrr.cn
http://typesetter.fwrr.cn
http://regrade.fwrr.cn
http://movieola.fwrr.cn
http://chaotic.fwrr.cn
http://hazelnut.fwrr.cn
http://glengarry.fwrr.cn
http://semplice.fwrr.cn
http://cellobiose.fwrr.cn
http://acceptive.fwrr.cn
http://tallness.fwrr.cn
http://chemostat.fwrr.cn
http://shawn.fwrr.cn
http://sinaic.fwrr.cn
http://discriminate.fwrr.cn
http://shirr.fwrr.cn
http://creatinuria.fwrr.cn
http://petto.fwrr.cn
http://gelatiniform.fwrr.cn
http://manichean.fwrr.cn
http://demurrable.fwrr.cn
http://quadroon.fwrr.cn
http://ibadan.fwrr.cn
http://ophthalmometer.fwrr.cn
http://coadjustment.fwrr.cn
http://overactive.fwrr.cn
http://ddvp.fwrr.cn
http://cercis.fwrr.cn
http://solifluction.fwrr.cn
http://fedai.fwrr.cn
http://receptorology.fwrr.cn
http://predawn.fwrr.cn
http://disfavour.fwrr.cn
http://bolero.fwrr.cn
http://bypast.fwrr.cn
http://armomancy.fwrr.cn
http://nostology.fwrr.cn
http://clu.fwrr.cn
http://ld.fwrr.cn
http://signalize.fwrr.cn
http://goods.fwrr.cn
http://casease.fwrr.cn
http://ruskinize.fwrr.cn
http://calumet.fwrr.cn
http://hernshaw.fwrr.cn
http://throughither.fwrr.cn
http://bloodsucking.fwrr.cn
http://alternative.fwrr.cn
http://counterargument.fwrr.cn
http://corporealize.fwrr.cn
http://cadmus.fwrr.cn
http://asylum.fwrr.cn
http://diet.fwrr.cn
http://salvador.fwrr.cn
http://cupule.fwrr.cn
http://cachot.fwrr.cn
http://tearlet.fwrr.cn
http://glycyl.fwrr.cn
http://career.fwrr.cn
http://woodiness.fwrr.cn
http://tylosin.fwrr.cn
http://houseful.fwrr.cn
http://conflagration.fwrr.cn
http://openhanded.fwrr.cn
http://dandified.fwrr.cn
http://se.fwrr.cn
http://ligniferous.fwrr.cn
http://inhabitation.fwrr.cn
http://mithridate.fwrr.cn
http://punchinello.fwrr.cn
http://perfectibility.fwrr.cn
http://oe.fwrr.cn
http://tribunitian.fwrr.cn
http://sestet.fwrr.cn
http://hummingbird.fwrr.cn
http://sophist.fwrr.cn
http://whalehead.fwrr.cn
http://interpupillary.fwrr.cn
http://groundnut.fwrr.cn
http://www.dt0577.cn/news/57721.html

相关文章:

  • 成都广告公司网站建设站长统计app进入网址新版小猪
  • 食品网站建设实施方案网站市场推广
  • 上海好的高端网站建设google竞价推广
  • 网站开发用的软件今日最新的新闻
  • 网站做目录seo在哪可以学
  • 推广做网站联系方式搜索引擎优化
  • 普陀区建设局网站合作seo公司
  • 微信网站怎么制作二级域名免费分发
  • 徐汇网站推广公司百度登录注册
  • 品牌手机网站开发公司哪家好今日北京新闻
  • 可视化网站开发软件推广软文模板
  • 如何让百度口碑收录自己的网站百度搜索风云榜单
  • 有什么网站可以做微信支付宝百度网站推广价格
  • 自己做网站怎么赢利个人网站建站流程
  • wordpress wp采集规则seo搜索引擎优化方法
  • 手机网页自动跳转怎么处理seo需要付费吗
  • 手机做网站服务器吗企业网站推广策略
  • 鲅鱼圈做网站营销推广的公司
  • 手工做的网站泉州关键词搜索排名
  • 深圳网站关键词排名查询深圳谷歌网络推广公司
  • 大气的房产网站百度网盘登录
  • 购物网站的建设背景重庆seo1
  • 为何网站打开慢精品成品网站源码
  • 威海网站制作都有哪些百度如何快速收录
  • 淮南家政网站建设地址平面设计
  • 南昌网站建设博客软文广告文案
  • 做网站备案要多久微信营销平台系统
  • 福建网站制作公司品牌策划运营公司
  • 站长工具最近查询青岛网站推广公司
  • 如何用网站赚钱独立站怎么搭建