通过引用返回值 [英] return value by reference
问题描述
大家好
在什么条件下我需要(或者桅杆)a(模板)函数返回值
参考?
你能给我一个例子
谢谢
hi all
In what condition i need( or mast) a (templates)function return value by
reference?
can you give me a example
thanks
推荐答案
" huangshan" < hu ************** @ hotmail.comwrote:
"huangshan" <hu**************@hotmail.comwrote:
在什么条件下我需要(或桅杆)a(模板)函数返回值由
参考?
你能给我一个例子
In what condition i need( or mast) a (templates)function return value by
reference?
can you give me a example
如果我理解这个问题,让它返回什么时候它返回的是什么呢?b
返回的是调用代码合法已经拥有的东西
访问权限(例如;传递给它的东西)它作为参考
参数,或当它返回''* this''时,或者它是什么时候
成员函数和类保证返回的对象将至少存在,直到下一个成员函数被调用(在这种情况下返回
a const reference)。
>
-
要给我发电子邮件,请输入sheltie在主题中。
If I understand the question, have it return by reference when what it
is returning is something that the calling code legitimately already has
access to (e.g.; something that was passed to it as a reference
parameter, or when it''s returning ''*this'',) or when it''s a
member-function and the class guarantees that the object returned will
exist at least until the next member-function is called (make it return
a const reference in this case.)
--
To send me email, put "sheltie" in the subject.
class1& function1()
{
class1 * c = new class1;
return * c;
}
huangshan写道:
class1& function1()
{
class1* c=new class1;
return *c;
}
huangshan wrote:
hi all
在什么条件下我需要(或桅杆)a(模板)函数返回值
参考?
你能举个例子
谢谢
hi all
In what condition i need( or mast) a (templates)function return value by
reference?
can you give me a example
thanks
huangshan写道:
huangshan wrote:
hi all
在什么条件下我需要(或桅杆)a(模板)函数返回值
参考?
你能给我一个例子
谢谢
hi all
In what condition i need( or mast) a (templates)function return value by
reference?
can you give me a example
thanks
如果你提供了一个简单的例子 - 练习本来就很远
更适合您。
模板与否,完全相同的规则适用。
条件
a)返回的对象/变量必须有效(未销毁)
b)何时需要引用或const引用?
让我们举一个例子,或许可以消除返回的内容。意思是:
无效增量(int& r_n)
{
++ r_n;
}
这个函数看起来没有返回任何东西,但它实际上是
,因为它修改了一个提供的参数,而不是* local *
该参数的副本。所以返回通过将参考作为参数来实现参考通常是安全的。
int& increment(int n)//错误代码
{
返回n;
}
该函数未定义行为,因为整数n(本地副本)
在函数返回时不再存在。返回引用
垃圾是垃圾(TM)。
最后,也许还有一点点相关性,一个* const *引用是
安全地返回班级私人成员的安全方式。在这种情况下,
很明显,n是有效的,但这里的目标是不允许有人通过get()意外修改私人成员
- 只是因为他们
有一个对它的引用。
template< typename T>
class N
{
T t;
public:
N():t(){}
const T& get()const {return t; }
};
重要的是要注意const说明符非常重要
这里。如果你要实现没有const说明符的get(),
你就能得到像get()= 6这样令人讨厌的东西,并且不小心
修改成员t引用 - 非常非常糟糕的副作用。
所以它不仅仅是当返回引用有效时。不再但是
而不是什么时候才有意义返回什么类型的参考
(const或不?)。
If you had provided a simple example - the exercise would have been far
more relevent for you.
Template or not, the exact same rules apply.
The "conditions" are
a) that the object/variable returned must be valid (not destroyed)
b) when is a reference, or a const reference, required?
Lets take an example and perhaps dispell what "returning" means:
void increment(int& r_n)
{
++r_n;
}
This function looks like its not returning anything, but it actually is
in the sense that its modifying a supplied parameter, not a *local*
copy of that parameter. So "returning" a reference is often safely
accomplished by having a reference as a parameter.
int& increment(int n) // bad code
{
return n;
}
That function is undefined behavious since integer n (a local copy)
ceases to exist when the function returns. Returning a reference to
garbage is garbage (TM).
Finally, and perhaps a tad more relevent, a *const* reference is the
safe way to return a private member of a class - safely. In this case,
obviously, n is valid but the goal here is not to allow somebody to
accidentally modify the private member via get() - just because they
have a reference to it.
template< typename T >
class N
{
T t;
public:
N() : t() { }
const T& get() const { return t; }
};
Its important to note that the const specifiers are very important
here. If you were to implement get() without the const specifiers,
you''ld be able to something nasty like get() = 6 and accidentally
modify member t through the reference - very,very bad side effect.
So its not just about "when returning a reference is valid" anymore but
rather "when does it make sense" to return what type of reference
(const or not?).
这篇关于通过引用返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!