在 C++ 中通过引用/值传递 [英] Pass by Reference / Value in C++

查看:38
本文介绍了在 C++ 中通过引用/值传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清按值和按引用之间的区别.

I would like to clarify the differences between by value and by reference.

我画了一张图

所以,对于按值传递,

使用不同的引用创建相同对象的副本,并为局部变量分配新引用,因此指向新副本

a copy of an identical object is created with a different reference, and the local variable is assigned the new reference, so to point to the new copy

如何理解单词:"如果函数修改了那个值,修改也会出现在按值和按引用传递的调用函数范围内"

How to understand the words: " If the function modifies that value, the modifications appear also within the scope of the calling function for both passing by value and by reference "

谢谢!

推荐答案

我认为不传达通过引用传递的含义会产生很多混乱.当有些人说通过引用传递时,他们通常指的不是参数本身,而是被引用的对象.其他人说通过引用传递意味着不能在被调用者中更改对象.示例:

I think much confusion is generated by not communicating what is meant by passed by reference. When some people say pass by reference they usually mean not the argument itself, but rather the object being referenced. Some other say that pass by reference means that the object can't be changed in the callee. Example:

struct Object {
    int i;
};

void sample(Object* o) { // 1
    o->i++;
}

void sample(Object const& o) { // 2
    // nothing useful here :)
}

void sample(Object & o) { // 3
    o.i++;
}

void sample1(Object o) { // 4
    o.i++;
}

int main() {
    Object obj = { 10 };
    Object const obj_c = { 10 };

    sample(&obj); // calls 1
    sample(obj) // calls 3
    sample(obj_c); // calls 2
    sample1(obj); // calls 4
}

有些人会声称 1 和 3 是按引用传递,而 2 是按值传递.另一群人说除了最后一个都是通过引用传递的,因为对象本身没有被复制.

Some people would claim that 1 and 3 are pass by reference, while 2 would be pass by value. Another group of people say all but the last is pass by reference, because the object itself is not copied.

我想在这里定义我声称的通过引用传递.可以在此处找到对其的一般概述:引用传递和值传递的区别.第一个和最后一个是值传递,中间两个是引用传递:

I would like to draw a definition of that here what i claim to be pass by reference. A general overview over it can be found here: Difference between pass by reference and pass by value. The first and last are pass by value, and the middle two are pass by reference:

    sample(&obj);
       // yields a `Object*`. Passes a *pointer* to the object by value. 
       // The caller can change the pointer (the parameter), but that 
       // won't change the temporary pointer created on the call side (the argument). 

    sample(obj)
       // passes the object by *reference*. It denotes the object itself. The callee
       // has got a reference parameter.

    sample(obj_c);
       // also passes *by reference*. the reference parameter references the
       // same object like the argument expression. 

    sample1(obj);
       // pass by value. The parameter object denotes a different object than the 
       // one passed in.

我投票支持以下定义:

当且仅当被调用函数的相应参数具有引用类型并且引用参数直接绑定到参数表达式(8.5.3)时,通过引用传递参数(1.3.1)/4).在所有其他情况下,我们必须处理按值传递.

An argument (1.3.1) is passed by reference if and only if the corresponding parameter of the function that's called has reference type and the reference parameter binds directly to the argument expression (8.5.3/4). In all other cases, we have to do with pass by value.

这意味着以下是按值传递的:

That means that the following is pass by value:

void f1(Object const& o);
f1(Object()); // 1

void f2(int const& i);
f2(42); // 2

void f3(Object o);
f3(Object());     // 3
Object o1; f3(o1); // 4

void f4(Object *o);
Object o1; f4(&o1); // 5

1 是按值传递的,因为它不是直接绑定的.实现可以复制临时文件,然后将该临时文件绑定到引用.2 是按值传递的,因为实现初始化了文字的临时值,然后绑定到引用.3 是传值,因为参数没有引用类型.4 出于同样的原因是按值传递的.5 是传值,因为参数没有引用类型.以下案例为引用传递(根据8.5.3/4等规则):

1 is pass by value, because it's not directly bound. The implementation may copy the temporary and then bind that temporary to the reference. 2 is pass by value, because the implementation initializes a temporary of the literal and then binds to the reference. 3 is pass by value, because the parameter has not reference type. 4 is pass by value for the same reason. 5 is pass by value because the parameter has not got reference type. The following cases are pass by reference (by the rules of 8.5.3/4 and others):

void f1(Object *& op);
Object a; Object *op1 = &a; f1(op1); // 1

void f2(Object const& op);
Object b; f2(b); // 2

struct A { };
struct B { operator A&() { static A a; return a; } };
void f3(A &);
B b; f3(b); // passes the static a by reference

这篇关于在 C++ 中通过引用/值传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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