Three parameter transfer methods in C++
Value pass:
The most common method of passing parameters is that the formal parameters of a function are copies of real parameters. Changing the formal parameters in the function will not affect the formal parameters outside the function. Generally, when the function changes parameters internally without affecting the caller, value passes will be used.
Pointer pass
A formal parameter is a pointer to the address of the actual parameter. As the name suggests, when operating the content pointed to by the formal parameter in a function, the actual parameter itself will be modified.
Reference pass
In C++, a reference is an alias for a variable, which is actually the same thing, and there is the same address in memory. In other words, no matter where the reference operation is, the referenced variable is operated fairly directly.
Let's see the demo:
#include <iostream> //Value transfervoid func1(int a) { std::cout << "Value passing, variable address:" << &a << ", variable value:" << a << std::endl; a ++ ; } //Pointer passvoid func2 (int* a) { std::cout << "Pointer pass, variable address:" << a << ", variable value:" << *a << std::endl; *a = *a + 1; } //Reference passvoid func3 (int& a) { std::cout << "Pointer pass, variable address:" << &a << ", variable value:" << a << std::endl; a ++; } int main() { int a = 5; std::cout << "Variable Actual Address:" << &a << ", variable value:" << a << std::endl; func1(a); std::cout << "After the value of the value is passed, the variable value:" << a << std::endl; std::cout << "Variable Actual Address:" << &a << ", variable value:" << a << std::endl; func2(&a); std::cout << "After pointer pass operation, variable value:" << a << std::endl; std::cout << "Variable Actual Address:" << &a << ", variable value:" << a << std::endl; func3(a); std::cout << "After the reference pass operation, the variable value:" << a << std::endl; return 0; }
The output result is as follows:
The actual address of the variable: 0x28feac, the value of the variable: 5
Value passed, variable address: 0x28fe90, variable value: 5
After the value transfer operation, the variable value: 5
The actual address of the variable: 0x28feac, the value of the variable: 5
Pointer pass, variable address: 0x28feac, variable value: 5
After pointer pass operation, variable value: 6
The actual address of the variable: 0x28feac, the value of the variable: 6
Pointer pass, variable address: 0x28feac, variable value: 6
After the reference pass operation, the value of the variable: 7
Parameter passing in Go
The above introduces three parameter transfer methods of C++. Value transfer and pointer transfer are easy to understand. So does Go also have these parameter transfer methods? This has caused controversy, but compared with the concept of reference passing in C++, we can say that Go has no reference passing method. Why do you say that, because Go does not have the concept of reference to variables. But Go has a reference type, which will be explained later.
Let’s first look at an example of Go passing value and pointer:
package main import ( "fmt" ) func main() { a := 1 ( "Variable Actual Address:", &a, "Variable value:", a) func1 (a) ( "After the value of the value is passed, the variable value:", a) ( "Variable Actual Address:", &a, "Variable value:", a) func2(&a) ( "After pointer pass operation, variable value:", a) } //Value transferfunc func1 (a int) { a++ ( "Value passing, variable address:", &a, "Variable value:", a) } //Pointer passfunc func2 (a *int) { *a = *a + 1 ( "Pointer pass, variable address:", a, "Variable value:", *a) }
The output result is as follows:
The actual address of the variable: 0xc04203c1d0 Variable value: 1
Value passed, variable address: 0xc04203c210 Variable value: 2
After the value transfer operation, the value of the variable: 1
The actual address of the variable: 0xc04203c1d0 Variable value: 1
Pointer pass, variable address: 0xc04203c1d0 Variable value: 2
After pointer pass operation, variable value: 2
It can be seen that the value passing and pointer passing of Go primitive types are no different from C++, but it does not have the concept of reference to variables. So how do you understand Go's reference type?
Go's reference type
In Go, reference types include slices, dictionaries, channels, etc. Taking slices as an example, is it a reference to pass a slice?
For example:
package main import ( "fmt" ) func main() { m1 := make([]string, 1) m1[0] = "test" ("Call func1 first m1 value:", m1) func1(m1) ("M1 value after calling func1:", m1) } func func1 (a []string) { a[0] = "val1" ("func1:", a) }
The output result is as follows:
Call func1 before m1 value: [test]
func1: [val1]
After calling func1, m1 value: [val1]
Modifications made to slices in the function affect the value of the actual parameter. Is this a quote passed on?
Actually, it is not. To answer this question, you must first figure out whether calling function slice m1 has changed. First of all, we must recognize the essence of slices.
A slice is a description of an array fragment. It contains a pointer to the array and the length of the fragment.
That is to say, what we print above is not the slice itself, but the array pointed to by the slice. Let me give you another example to verify whether the slice has changed.
package main import ( "fmt" ) func main() { m1 := make([]string, 1) m1[0] = "test" ("Call func1 first m1 value:", m1, cap(m1)) func1(m1) ("M1 value after calling func1:", m1, cap(m1)) } func func1 (a []string) { a = append(a, "val1") ("func1:", a, cap(a)) }
The output result is as follows:
Call func1 before m1 value: [test] 1
func1: [test val1] 2
After calling func1, m1 value: [test] 1
This result shows that the slice before and after the call has not changed. The so-called "change" in the previous example actually changed the elements of the array pointed to by the pointer to the array in the slice. This sentence may be difficult to pronounce, but it is actually the case. Once again, the parameter passing of the reference type is not a pass-by-reference .
Want to have a thorough understanding of a slice is a description of an array fragment. It contains the sentence "Pointer to the array and the length of the fragment". If you are interested, you can read this article:https:///kf/201604/. Learn the memory model of slices.
Summarize
The summary is simple, and language also needs to see the essence through phenomena. There are also the conclusions of this article that need to be remembered:
There is no pass-by-reference in Go.
The above is the editor’s introduction to whether there is any reference to the Go language (compare C++)? I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!