是否存在任何语言的 REAL 引用传递?
Does there exist REAL pass by reference in any language?
据我所知,c 中没有按引用传递,java 基本上按值传递所有内容,有许多堆栈溢出帖子讨论这个问题。
现在我想知道有没有REAL call by reference的例子?因为在函数调用期间,所有参数(包括指针或可变对象标识符)的值总是被复制到被调用者框架中的局部变量,从这个意义上说,一切都肯定是按值传递的。
当然有。例如,C♯ 具有按引用传递。为了实现按引用传递,声明站点参数列表中的方法参数以及调用站点参数列表中的方法调用参数都必须使用 ref
修饰符进行注释。这同样适用于 Visual Basic.NET(我相信这里的修饰符是 ByRef
。)
C++也有引用传递,修饰符是&
。 PHP 也有传递引用并使用相同的修饰符。同样适用于E.
Rust 还提供按引用调用。
与上面列出的所有语言不同,其中按值传递是默认值并且必须明确请求按引用传递,Fortran II 是一种按引用传递语言。
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♯的例子,演示值类型的值传递、引用类型的值传递、值类型的引用传递和引用传递参考类型:
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.
}
}
据我所知,c 中没有按引用传递,java 基本上按值传递所有内容,有许多堆栈溢出帖子讨论这个问题。
现在我想知道有没有REAL call by reference的例子?因为在函数调用期间,所有参数(包括指针或可变对象标识符)的值总是被复制到被调用者框架中的局部变量,从这个意义上说,一切都肯定是按值传递的。
当然有。例如,C♯ 具有按引用传递。为了实现按引用传递,声明站点参数列表中的方法参数以及调用站点参数列表中的方法调用参数都必须使用 ref
修饰符进行注释。这同样适用于 Visual Basic.NET(我相信这里的修饰符是 ByRef
。)
C++也有引用传递,修饰符是&
。 PHP 也有传递引用并使用相同的修饰符。同样适用于E.
Rust 还提供按引用调用。
与上面列出的所有语言不同,其中按值传递是默认值并且必须明确请求按引用传递,Fortran II 是一种按引用传递语言。
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♯的例子,演示值类型的值传递、引用类型的值传递、值类型的引用传递和引用传递参考类型:
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.
}
}