本文介绍了分配对变量的引用..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

当我这样做时:

someclass& someobject = someotherobject;


它将someotherobject的引用转换为变量''someobject''

你刚宣布。所以,基本上

someobject和someotherobject实际上是两个名字相同的变量。


但如果我这样做:


someclass someobject;

& someobject = someotherobject;


它不再有效D:

有谁知道如何让它在第二种情况下工作?


-

解决方案




这是不允许的。


此外,通常使用参考文件是受限制的

功能参数和返回类型,有时候是
作为班级成员。


你想具体做什么?


告诉我们,我们会告诉你如何。


BTW您正在阅读哪些C ++书籍?


-Mike






为了使上述有效,必须存在someclass类型的对象(或从中派生的类型)
名为someotherobject的对象。上面的陈述声明
''someobject''是对象''someotherobject''的引用。


名称的相同变量。

''someotherobject''是''someclass'类型的对象。
''someobject''成为''someotherobject''的''别名'',即两个名字都指的是同一个对象。

关键点:参考,当声明*必须*
绑定到一个对象。例如。你不能写:

someclass& someobject;

此外,一旦引用已被删除,它*不能*
以后绑定到不同的对象。对于在整个生命周期内用
初始化的对象,它仍然是''别名'。



这不合法。



这是不允许的。

此外,通常参考文献的使用仅限于功能参数和返回类型,有时还会作为班级成员使用。

你想具体做什么?

告诉我们,我们会告诉你如何。

BTW您正在阅读哪些C ++书籍?

- Mike




hi, when I do:
someclass &someobject = someotherobject;

it asigns the reference of someotherobject to the variable ''someobject'' that
you just declared. SO that basically
someobject and someotherobject are really the same variable with two names.

but if I do:

someclass someobject;
&someobject = someotherobject;

it no longer works D:
does anyone know how to get it to work in the second case?

--

解决方案



It''s not allowed.

Also, typically the use of references is restricted
to function parameters and return types, and sometimes
as class members.

What specifically do you want to do?

Tell us that, and we''ll tell you how.

BTW which C++ book(s) are you reading?

-Mike




No.

For the above to be valid, there must exist
an object of type ''someclass'' (or a type derived from it)
named ''someotherobject''. The statement above declares
''someobject'' to be a reference to the object ''someotherobject''.


names.

''someotherobject'' is an object of type ''someclass''.
''someobject'' becomes an ''alias'' for ''someotherobject'',
i.e. both names refer to the same object.

Critical point: A reference, when declared *must*
be bound to an object. E.g. you cannot write:

someclass& someobject;

Also, once a reference has been delcared, it *cannot*
later be bound to a different object. It stays being
an ''alias'' for the object it was initialized with
for its entire lifetime.



This is not legal.



It''s not allowed.

Also, typically the use of references is restricted
to function parameters and return types, and sometimes
as class members.

What specifically do you want to do?

Tell us that, and we''ll tell you how.

BTW which C++ book(s) are you reading?

-Mike





That''s not a very good way to learn C++. Frankly, most C++ teachers
don''t know the language well enough to be teaching it (but they usually
believe they do). A good book is about the best way.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于分配对变量的引用..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-09 01:44