C ++:在这种情况下引用的优点是什么? [英] C++: what is the advantage of references in this case?

查看:61
本文介绍了C ++:在这种情况下引用的优点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两段代码:

int f1(int b)
{
   return b;
}

int & f2(int b)
{
   return b;
}

这些功能之间有什么区别?我知道第二个函数返回一个引用,但是由于我可以以相同的方式使用这两个函数,所以有什么区别?

What is the difference between these functions? I know that the second one returns a reference, but since I can use both functions in the same way, what's the difference?

此功能更好吗?

int && f2(int b)
{
   return b;
}

我什么时候应该使用返回引用的函数?

And when should I use functions which return references?

Edit2:那我什么时候应该使用返回Rvalue引用的函数?

Then when should I use functions which return Rvalue references?

推荐答案

考虑一个简单的类,该类包装一个数组,仅是为了提供示例OP可以对返回的引用执行的操作.

Consider a simple class that wraps an array solely for the purpose of providing an example of what the OP can do with a returned reference.

class example
{
private:
    int array[]= {1,2,3,4,5,6,7,8,9,0};
public:
    int get(int index)
    {
        return array[index];
    }
    int & get2(int index)
    {
        return array[index];
    }
}

现在,我们有一个示例,它不会进入行为不确定的荒地,并且可以向您展示此全面装备和运营参考的力量.

Now we have an example that will not go into the badlands of undefined behaviour and can show you the power of this fully armed and operational reference.

说我们有

example x;

我们可以调用任一get函数来检索值

We can call either get function to retrieve a value

int val1 = x.get(1);
int val2 = x.get2(2)

但是我们也可以

x.get2(3) = 30;

因为get2返回一个引用,我们可以将其分配给它并使分配棒化.

because get2 returns a reference we can assign to it and make the assignment stick.

如果要在示例中添加索引运算符,这将是非常宝贵的

This is invaluable should you want to add an index operator to example

int & operator[](int index)
{
    return array[index];
}

因为它允许预期的数组行为

because it allows the expected array behaviour

int val = x[5];
x[6] = 10;

编辑

Tony D带来了另一个重要功能.返回引用将返回 by 引用.除了允许修改返回的对象外,它不会进行复制,并且节省了进行复制可能要花费的所有精力.对于整数的示例情况,这是没有意义的.传递整数和对整数的引用的成本将相同或相近,以至于无关紧要.对于较大,更复杂的对象(可能需要花费大量的精力才能复制)或无法或不应复制的对象,情况并非如此.

EDIT

Tony D brings up another important feature. Returning a reference returns by reference. In addition to allowing modification of the returned object, this does not make a copy and saves whatever effort would have been consumed by making a copy. For the example case of integers this is moot. The cost of passing an integer and a reference to an integer will either be the same or so close that it shouldn't matter. This is not true of a larger, more complex object that could take a significant amount of effort to copy or an object that cannot or should not be copied.

BigFreakingObject & getBigFreakingObject();

将允许调用方在 BigFreakingObject 上进行操作,而不会产生复制它的费用.但是,这会将密钥移交给王国,并且允许调用者执行 BigFreakingObject 的任何权限,而 BigFreakingObject 的权限将允许这样做,并且这可能与的要求冲突BigFreakingObject 的所有者.

will allow the caller to operate on a BigFreakingObject without incurring the costs of duplicating it. This hands over the keys to the kingdom however and allows the caller to do to BigFreakingObject whatever BigFreakingObject's permissions will allow, and this may conflict with the requirements of BigFreakingObject's owner.

使用

const BigFreakingObject & getBigFreakingObject();

BigFreakingObject const & getBigFreakingObject();

将提供对 BigFreakingObject 的引用,但不允许调用者修改其状态,从而保护 BigFreakingObject 的所有者免受任何不愉快的意外.

will provide a reference to a BigFreakingObject but not allow the caller to modify its state, protecting the owner of BigFreakingObject from any unpleasant surprises.

有关此问题的更多详细信息,请阅读有关常量正确性的内容.

For more details on this, read up on Const Correctness.

这篇关于C ++:在这种情况下引用的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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