两者有什么区别

引用传递的参数 参数通过value?

你能给我举几个例子吗?


当前回答

当通过引用传递时,基本上是传递一个指向变量的指针。通过值传递,即传递变量的副本。

在基本用法中,这通常意味着通过引用传递,对变量的更改将在调用方法中看到,而在通过值传递时则不会。

其他回答

在理解这两个术语之前,您必须了解以下内容。每一个物体都有两个可以使它被区分的东西。

它的价值。 它的地址。

如果你说employee。name = "John",要知道关于name有两件事。它的值是“John”,它在内存中的位置是一个十六进制数,可能像这样:0x7fd5d258dd00。

根据语言的体系结构或对象的类型(类、结构等),可以传输“John”或0x7fd5d258dd00

传递“John”称为按值传递。

传递0x7fd5d258dd00称为引用传递。任何指向这个内存位置的人都可以访问“John”的值。

有关这方面的更多信息,我建议您阅读有关取消指针引用的内容,以及为什么选择struct(值类型)而不是类(引用类型)。

A major difference between them is that value-type variables store values, so specifying a value-type variable in a method call passes a copy of that variable's value to the method. Reference-type variables store references to objects, so specifying a reference-type variable as an argument passes the method a copy of the actual reference that refers to the object. Even though the reference itself is passed by value, the method can still use the reference it receives to interact with—and possibly modify—the original object. Similarly, when returning information from a method via a return statement, the method returns a copy of the value stored in a value-type variable or a copy of the reference stored in a reference-type variable. When a reference is returned, the calling method can use that reference to interact with the referenced object. So, in effect, objects are always passed by reference.

In c#, to pass a variable by reference so the called method can modify the variable's, C# provides keywords ref and out. Applying the ref keyword to a parameter declaration allows you to pass a variable to a method by reference—the called method will be able to modify the original variable in the caller. The ref keyword is used for variables that already have been initialized in the calling method. Normally, when a method call contains an uninitialized variable as an argument, the compiler generates an error. Preceding a parameter with keyword out creates an output parameter. This indicates to the compiler that the argument will be passed into the called method by reference and that the called method will assign a value to the original variable in the caller. If the method does not assign a value to the output parameter in every possible path of execution, the compiler generates an error. This also prevents the compiler from generating an error message for an uninitialized variable that is passed as an argument to a method. A method can return only one value to its caller via a return statement, but can return many values by specifying multiple output (ref and/or out) parameters.

请参阅c#讨论和示例链接文本

例子:

class Dog 
{ 
public:
    barkAt( const std::string& pOtherDog ); // const reference
    barkAt( std::string pOtherDog ); // value
};

Const &通常是最好的。你不会受到建造和破坏的惩罚。如果引用不是const,你的接口暗示它将改变传入的数据。

看看这张照片:

在第一种情况下(通过引用传递),当变量在函数内部设置或更改时,外部变量也会更改。

但在第二种情况下(按值传递),改变函数内部的变量不会对外部变量产生任何影响。

要阅读这篇文章,请参见这个链接。

简而言之,通过值传递的是它是什么,通过引用传递的是它在哪里。

如果你的值是VAR1 = "Happy Guy!",你只会看到"Happy Guy!"如果VAR1变成了“Happy Gal!”,你就不会知道了。如果它是通过引用传递的,并且VAR1发生了变化,那么您就会这样做。