通过参考返回的意义是什么? [英] Whats the significance of return by reference?
本文介绍了通过参考返回的意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++中,
function()= 10;如果函数通过引用返回一个变量,则
。
$ b b
它的用例是什么?
解决方案
最常见的情况是实现像operator [ / p>
struct A {
int data [10];
int& operator [](int i){
return data [i];
}
};另一个是通过accesor函数从类返回一个大对象: $ b
$ b struct b {
SomeBigThing big;
const SomeBigThing& MyBig()const {
return big;
}
};
,以避免复制开销。
In C++,
function() = 10;
works if the function returns a variable by reference.
What are the use cases of it?
解决方案 The commonest case is to implement things like operator[].
struct A {
int data[10];
int & operator[]( int i ) {
return data[i];
}
};
Another is to return a big object from a class via an accesor function:
struct b {
SomeBigThing big;
const SomeBigThing & MyBig() const {
return big;
}
};
in order to avoid the copying overhead.
这篇关于通过参考返回的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文