指针与参考 [英] Pointer vs Reference

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

问题描述



我们可以确认以下内容吗?也有人说,Java也有

引用就像在C ++中,这是一个隐式指针:


指针和参考

-------------- -------


我开始看到什么指针和引用是什么以及它们如何与
相关

到彼此。

$ C $ b在C时代,指针*是*参考。这就是为什么当我们有


int a = 10;

int * pi =& a;


,你可以取消引用它:


* pi = 20;


直到C ++出现,我们才有一个新的参考:


int a = 10;

int i =&一个; //或int i =& a;我不确定语法。

i = 20; //现在我和我都是20

所以这种类型的引用是一个隐式指针......它指向a,

但是

你不能在C中使用这种方式(int * pi =& a)当你使用(i = 20)时,

它会默默地进行解除引用。 (* pi = 20;)


所以引用是新的:指针但是看起来不像指针。


来了考虑一下,在Java和Ruby中,它们也是如此。


a = Car.new


a看起来不像一个指针,但它实际上是一个指针。


我们不要取消引用它来获得像(* a).value = 10
$ b $这样的属性b或a-> value = 10但是只使用a.value = 10

所以从这一点开始,引用和指针不一样...... a br />
引用是一个指针,它看起来不像指针。


它们都指向某个东西。但是使用的语法(或语法)

看起来不像是C时代的指针。引用是

自动解除引用指针,我们要说什么?或隐含的

指针,或无声指针。指针。

PHP5中的


$ a = 10;

$ b =&美元; #now $ b含蓄地指向$ a

$ b = 20; #now $ b隐含地指向$ a,这是20


$ a = new Foo(hello); #$ a隐式指向Foo对象

#Foo对象是100字节,

#但$ a只有4个字节


$ b =&美元; #$ b隐式指向$ a。

#$ b是指向指针的指针

#$ b指向一个四字节指针,即$ a


$ b =新Foo(ok); #dereference $ b并将其内容设置为

#指向另一个对象的新指针Foo(ok)

#即$ a指向Foo(& ; ok")现在

#$ b仍然指向$ a,这指向

Foo(ok)


现在,当你打印$ b和$ a时,它们都是Foo(ok)

在我们现在使用的语言中,哪种语言有引用


在思考C ++,Java,(Perl?),Python,PHP5,Ruby都有参考。


在我们现在使用的语言中,哪种语言有参考"一个

"" ;?


我认为C ++,Java,PHP5确实...不确定Python和Ruby。

解决方案

a = 10;


b =&

一个; #now



Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":

Pointer and Reference
---------------------

I am starting to see what pointer and reference are and how they
relate
to each other.

in the C era, a pointer *is* a reference. that''s why when we have

int a = 10;
int *pi = &a;

and you can "dereference it":

*pi = 20;

Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.
i = 20; // now both a and i are 20

so this type of reference is an implicit pointer... it points to a,
but
you don''t use the way in C (int *pi = &a) And when you use (i = 20),
it does the dereference silently. (*pi = 20;)

so a reference is new: a pointer but "looks like not a pointer".

come to think about it, in Java and Ruby, they are like that too.

a = Car.new

a doesn''t look like a pointer, but it is actually a pointer.

we don''t dereference it to get to the attributes like (*a).value = 10
or a->value = 10 but just use a.value = 10

So from this point on, a reference and a pointer are not the same... a
reference is a pointer "that doesn''t look like a pointer."

they both points to something. but the syntax (or grammar) of usage
doesn''t look like it is a pointer in the C era. a reference is an
"automatically dereferenced" pointer, shall we say? or an "implicit"
pointer, or "silent" pointer.

in PHP5,

$a = 10;
$b =& $a; # now $b implicitly points to $a
$b = 20; # now $b implicitly points to $a, which is 20

$a = new Foo("hello"); # $a implicitly points to a Foo object
# the Foo object is 100 bytes,
# but $a is just 4 bytes

$b =& $a; # $b implicitly points to $a.
# $b is a pointer to pointer
# $b points to a four byte pointer, which is $a

$b = new Foo("ok"); # dereference $b and sets its content to
# a new pointer to another object Foo("ok")
# that is, $a points to Foo("ok") now
# $b still points to $a, which points to
Foo("ok")

So now, when you print $b and $a, they are both Foo("ok")
In language we use nowadays, which language has "reference"

In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.

In language we use nowadays, which language has "reference" to a
"reference"?

I think C++, Java, PHP5 do... not sure about Python and Ruby.

解决方案

a = 10;


b =&


a; # now


这篇关于指针与参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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