指针vs引用与值c ++的常规传递 [英] pointers vs. references vs. a regular pass by value c++

查看:171
本文介绍了指针vs引用与值c ++的常规传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在回答前阅读。我不想让你为我说明明显的。谢谢:D



我试图区分指针和传值。我认为我理解他们,但有一件事我读的是不清楚是什么是传递一个指针和通过值之间的区别。考虑以下...

  andy = 25; 
fred = andy;
ted =& andy;

第一行将值25传递给andy。第二行将andy的值传递给fred。现在fred拥有值25.第三行通过引用ted。现在看看下一个代码

  andy = 25; 
fred = andy;
ted =& andy;
joe = * andy;

上面的代码看起来就像第一个代码片段,除非我们将andy的指针传递给joe。现在joe保持指针值为25.这几乎看起来和刚刚做的一样(joe = andy)。没有*



与传递值相比,传递指针值有什么区别?



在我的解释中,似乎joe的指针值仍然受影响,如果后面的行我决定更改的值andy /比较如果我刚刚通过通过价值,它不会影响。 T



据我所知,传递引用和传递指针之间的唯一区别是一个保存地址,另一个保存该地址内的值。如果具有该值引用的任何变量仍然可以改变,则决定改变它。如果是这样,通过引用和通过值传递的意义是什么。



这里是我正在阅读的链接



http://www.cplusplus.com/doc/tutorial/pointers/

解决方案 c $ c>因为andy不是指针,它是一个int。



但是,你可以做 joe = * ted ,因为ted是一个指针。



假设你这样做:

  andy = 25; 
fred = andy;
ted =& andy;
joe = * ted;
andy = 26;
jim = * ted;

然后joe的值为25,而jim的值为26。 >

这样做的原因是ted是指向andy的指针。使用 * ted 为您提供该指针的值。


Please read before answering. I dont want you to state the obvious for me. THANKS :D

I am trying to differentiate between pointers and passing by value. And I think I understand them but one thing the source I was reading wasnt clear on is what is the difference between passing a pointer and passing by value. consider the following...

andy = 25;
fred = andy;
ted = &andy; 

The first line passes the value 25 to andy. The second line passes the value of andy to fred. Now fred possess the value 25. the third line passes reference to ted. Now ted holds the memory address of andy, which holds the value of 25. Now take a look at the next code

andy = 25;
fred = andy;
ted = &andy; 
joe = *andy;

The above code looks just like the first snippet except we pass the pointer of andy to joe. Now joe holds the pointer value of 25. Which almost seems the same as just doing (joe = andy). without *

what's the difference in passing the pointer value compared to just passing by value ???

In my interpretation, It appears that the pointer value of joe is still affected if later down the line I decided to change the value of andy/ compared to if I just passed by value, it not be affect. T

To my knowledge, the only difference between passing by reference and passing by pointer is that one holds the address, and the other holds the value within that address. which can still change if any varible that holds that value reference, decides to change it. If this is the case, what is the significance of passing by reference and passing by value.

Here is the link to what I was reading

http://www.cplusplus.com/doc/tutorial/pointers/

Thanks guys!!!

解决方案

You can't do joe = *andy because andy isn't a pointer, it's an int.

However, you could do joe = *ted, because ted is a pointer.

Suppose you did this:

andy = 25; 
fred = andy; 
ted = &andy;  
joe = *ted;
andy = 26;
jim = *ted;

Then joe will have a value of 25, while jim will have a value of 26.

The reason for this is that ted is a pointer to andy. Using *ted gives you the value at that pointer, at that time.

这篇关于指针vs引用与值c ++的常规传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆