刺猬在天冷时围抱取暖,但保持一定距离避|

jinganglang567

园龄:4年5个月粉丝:0关注:20

📂c++
🔖c++
2023-02-13 09:23阅读: 25评论: 0推荐: 0

运算符重载

运算符重载

C++将运算符重载扩展到自定义的数据类型,它可以让对象操作更美观。

例如字符串string用加号(+)拼接、cout用两个左尖括号(<<)输出。

运算符重载函数的语法:

返回值 operator运算符(参数列表);

运算符重载函数的返回值类型要与运算符本身的含义一致。

非成员函数版本的重载运算符函数:形参个数与运算符的操作数个数相同;

成员函数版本的重载运算符函数:形参个数比运算符的操作数个数少一个,其中的一个操作数隐式传递了调用对象。

如果同时重载了非成员函数和成员函数版本,会出现二义性。

注意:

1)返回自定义数据类型的引用可以让多个运算符表达式串联起来。(不要返回局部变量的引用)

2)重载函数参数列表中的顺序决定了操作数的位置。

3)重载函数的参数列表中至少有一个是用户自定义的类型,防止程序员为内置数据类型重载运算符。

4)如果运算符重载既可以是成员函数也可以是全局函数,应该优先考虑成员函数,这样更符合运算符重载的初衷。

5)重载函数不能违背运算符原来的含义和优先级。

6)不能创建新的运算符。

7)以下运算符不可重载:

l sizeof sizeof运算符

. 成员运算符

.*    成员指针运算符

::    作用域解析运算符

?:    条件运算符

typeid    一个RTTI运算符

const_cast    强制类型转换运算符

dynamic_cast    强制类型转换运算符

reinterpret_cast   强制类型转换运算符

static_cast    强制类型转换运算符

8)以下运算符只能通过成员函数进行重载:

= 赋值运算符

() 函数调用运算符

[] 下标运算符

-> 通过指针访问类成员的运算符

本文作者:jinganglang567

本文链接:https://www.cnblogs.com/tgfoven/p/17115290.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   jinganglang567  阅读(25)  评论(0编辑  收藏  举报
  1. 1 look after you louis tomlinson
  2. 2 just hold on louis tomlinson
  3. 3 i can steven cooper
  4. 4 rock me one direction
  5. 5 just can't let her go one direction
  6. 6 this town Niall Horan
  7. 7 cut in love july
  8. 8 nemo 夜愿
  9. 9 in the end Black Veil Brides
  10. 10 glad you came the wanted
  11. 11 chasing the sun the wanted
  12. 12 TAKE MY HAND simple plan
  13. 13 wish i had an angel 夜愿
just hold on - louis tomlinson
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Sasha Alex Sloan/Eric Rosse

作曲 : Steve Aoki/Louis Tomlinson/Sasha Alex Sloan/Nolan Lambroza/Eric Rosse

Wish that you could build a time machine

So you could see

The things no one can see

Feels like you're standing on the edge

Looking at the stars

And wishing you were them

What do you do when a chapter ends?

Do you close the book and never read it again?

Where do you go when your story's done?

You can be who you were or who you'll become

Oooh

If it all goes wrong

Oooh

Darling just hold on

The sun goes down and it comes back up

The world it turns no matter what

Oooh

If it all goes wrong

Darling just hold on

Oooh

Darling just hold on

Oooh

It's not over until it's all been said

It's not over until your dying breath

So what do you want them to say when you're gone?

That you gave up or that you kept going on?

What do you do when a chapter ends?

Do you close the book and never read it again?

Where do you go when your story's done?

You can be who you were or who you'll become

Oooh

If it all goes wrong

Oooh

Darling just hold on

The sun goes down and it comes back up

The world it turns no matter what

Oooh

If it all goes wrong

Darling just hold on

Oooh

Darling just hold on

Oooh

Oooh

If it all goes wrong

Oooh

Darling just hold on

Oooh

If it all goes wrong

Darling just hold on

点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起