是否存在任何语言中的 REAL 引用传递? [英] Does there exist REAL pass by reference in any language?

查看:63
本文介绍了是否存在任何语言中的 REAL 引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,c 中没有按引用传递,而 Java 基本上按值传递所有内容,有数十篇堆栈溢出帖子对此进行了讨论.

As far as I know there's no pass by reference in c and java essentially passes everything by value, there're dozens of stack overflow posts discussing about this.

现在我想知道是否有任何通过引用进行 REAL 调用的示例?因为在函数调用过程中,所有参数(包括指针或可变对象标识符)的值总是被复制到被调用者框架中的局部变量中,从这个意义上说,一切都是按值传递的.

Now I wonder that is there any example of REAL call by reference? Because during function call the value of all parameters (including pointers or mutable object identifiers) are always copied to local variables in callee's frame, in that sense everything surely passes by value.

推荐答案

当然有.例如,C♯ 有传递引用.为了发生引用传递,声明站点的参数列表中的方法参数以及调用站点的参数列表中的方法调用参数都必须用 ref 修饰符.这同样适用于 Visual Basic.NET(这里的修饰符是 ByRef,我相信.)

Sure, there is. For example, C♯ has pass-by-reference. In order for pass-by-reference to occur, both the method parameter in the parameter list at the declaration site as well as the method call argument in the argument list at the call site must be annotated with the ref modifier. The same applies to Visual Basic.NET (here, the modifier is ByRef, I believe.)

C++ 也有传递引用,修饰符是 &.PHP 也有传递引用并使用相同的修饰符.这同样适用于 E.

C++ also has pass-by-reference, the modifier is &. PHP also has pass-by-reference and uses the same modifier. The same applies to E.

Rust 还提供按引用调用.

Rust also offers call-by-reference.

与上面列出的所有语言相比,按值传递是默认值并且必须显式请求按引用传递,而 Fortran II 是一种按引用传递语言.

In contrast to all the languages listed above, where pass-by-value is the default and pass-by-reference has to be explicitly requested, Fortran II is a pass-by-reference language.

现在我想知道是否有任何通过引用进行 REAL 调用的示例?因为在函数调用过程中,所有参数(包括指针或可变对象标识符)的值总是被复制到被调用者框架中的局部变量中,从这个意义上说,一切都是按值传递的.

Now I wonder that is there any example of REAL call by reference? Because during function call the value of all parameters (including pointers or mutable object identifiers) are always copied to local variables in callee's frame, in that sense everything surely passes by value.

您描述的是传值.这不是传递引用.通过引用传递,引用本身被传递,而不是被引用的值.

What you describe is pass-by-value. That's not pass-by-reference. With pass-by-reference, the reference itself is passed, not the value that is referenced.

这里是 C♯ 中的一个例子,它演示了值类型的传值、引用类型的传值、值类型的传引用和传值类型的传引用引用类型:

Here is an example in C♯ that demonstrates pass-by-value of a value type, pass-by-value of a reference type, pass-by-reference of a value type, and pass-by-reference of a reference type:

struct MutableCell
{
    public string value;
}

class Program
{
    static void IsCSharpPassByValue(string[] foo, MutableCell bar, ref string baz, ref MutableCell qux)
    {
        foo[0] = "More precisely, for reference types it is call-by-object-sharing, which is a special case of pass-by-value.";
        foo = new string[] { "C# is not pass-by-reference." };

        bar.value = "For value types, it is *not* call-by-sharing.";
        bar = new MutableCell { value = "And also not pass-by-reference." };

        baz = "It also supports pass-by-reference if explicitly requested.";

        qux = new MutableCell { value = "Pass-by-reference is supported for value types as well." };
    }

    static void Main(string[] args)
    {
        var quux = new string[] { "Yes, of course, C# *is* pass-by-value!" };

        var corge = new MutableCell { value = "For value types it is pure pass-by-value." };

        var grault = "This string will vanish because of pass-by-reference.";

        var garply = new MutableCell { value = "This string will vanish because of pass-by-reference." };

        IsCSharpPassByValue(quux, corge, ref grault, ref garply);

        Console.WriteLine(quux[0]);
        // More precisely, for reference types it is call-by-object-sharing, which is a special case of pass-by-value.

        Console.WriteLine(corge.value);
        // For value types it is pure pass-by-value.

        Console.WriteLine(grault);
        // It also supports pass-by-reference if explicitly requested.

        Console.WriteLine(garply.value);
        // Pass-by-reference is supported for value types as well.
    }
}

这篇关于是否存在任何语言中的 REAL 引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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