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

jinganglang567

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

📂c++
🔖c++
2023-02-10 15:44阅读: 32评论: 0推荐: 0

引用与函数

引用用于函数的参数

把函数的形参声明为引用,调用函数的时候,形参将成为实参的别名。
这种方法也叫按引用传递或传引用。(传值、传地址、传引用只是说法不同,其实都是传值。)
引用的本质是指针,传递的是变量的地址,在函数中,修改形参会影响实参。
1)传引用的代码更简洁。
2)传引用不必使用二级指针。
3)引用的属性和特别之处。

引用的形参和const

如果引用的数据对象类型不匹配,当引用为const时,C++将创建临时变量,让引用指向临时变量。
什么时候将创建临时变量呢?
l 引用是const。
l 数据对象的类型是正确的,但不是左值。
l 数据对象的类型不正确,但可以转换为正确的类型。
结论:如果函数的实参不是左值或与const引用形参的类型不匹配,那么C++将创建正确类型的匿名变量,将实参的值传递给匿名变量,并让形参来引用该变量。
将引用形参声明为const的理由有三个:
l 使用const可以避免无意中修改数据的编程错误。
l 使用const使函数能够处理const和非const实参,否则将只能接受非const实参。
l 使用const,函数能正确生成并使用临时变量。
左值是可以被引用的数据对象,可以通过地址访问它们,例如:变量、数组元素、结构体成员、引用和解引用的指针。
非左值包括字面常量(用双引号包含的字符串除外)和包含多项的表达式。

引用用于函数的返回值

传统的函数返回机制与值传递类似。

函数的返回值被拷贝到一个临时位置(寄存器或栈),然后调用者程序再使用这个值。

double m=sqrt(36);   // sqrt()是求平方根函数。
//sqrt(36)的返回值6被拷贝到临时的位置,然后赋值给m。
cout << sqrt(25);
//sqrt(25)的返回值5被拷贝到临时的位置,然后传递给cout。

如果返回的是一个结构体,将把整个结构体拷贝到临时的位置。
如果返回引用不会拷贝内存。
语法:
返回值的数据类型& 函数名(形参列表);
注意:
l 如果返回局部变量的引用,其本质是野指针,后果不可预知。
l 可以返回函数的引用形参、类的成员、全局变量、静态变量。
l 返回引用的函数是被引用的变量的别名,将const用于引用的返回类型。

本文作者:jinganglang567

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

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

posted @   jinganglang567  阅读(32)  评论(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

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