通过值和引用混淆参数
confused about parameters by value and by reference
分配--
创建一个至少包含三个参数的示例函数。至少有一个参数应该通过值传递,至少有两个参数应该以引用传递。
我创造了以下内容,但我很困惑,如果我做的是正确的事情,我对价值和参考感到困惑。
我在int中设置了num1=30,但将7传递给addThree。这是按值考虑的吗?
#include <iostream>
using namespace std;
//prototype
void addThree (int num1, int& num2, int& num3);
int main()
{
int num1 = 30;
int num2 = 50;
int num3 = 80;
addThree(7, num2, num3);
return 0;
}
void addThree (int num1, int& num2, int& num3)
{
int x;
int y;
int z;
x = num1 + 3;
y = num2 + 3;
z = num3 + 3;
cout << "x value is " << x << endl;
cout << "y value is " << y << endl;
cout << "z value is " << z << endl;
}
打印
x值为10
y值为53
z值为83
您的代码没有什么意义,因为据我所知,您必须演示按值传递和按引用传递之间的区别。
你可以考虑我将显示的代码作为你的代码的想法
#include <iostream>
void Multiply( int &x, int &y, int n )
{
n /= 2;
x *= n;
y *= n;
}
int main()
{
int x, y;
int n;
std::cout << "Enter x: ";
std::cin >> x;
std::cout << "Enter y: ";
std::cin >> y;
std::cout << "Enter factor: ";
std::cin >> n;
std::cout << "Before the multiplication. x = " << x << ", y = " << y << std::endl;
Myltiply( x, y, n );
std::cout << "After the multiplication. x = " << x << ", y = " << y << std::endl;
}
通过引用,您正在用另一个名称调用某个东西。根据价值,你可以复制一份
好吧,我真的不知道你在问什么,但这里有一个关于按值传递和按引用传递的简要概述。
在C++中,引用基本上是"安全"的指针。在维基百科中,它们被定义为:
在C++编程语言中,引用是一个简单的引用功能较弱但比指针类型更安全的数据类型遗传自C.
您使用与号来声明引用。您必须指定引用引用的实际变量。
int A = 5; // Created an actual integer
int& rA = A; // Created a reference that refers to the integer I declared earlier
当您通过引用将某些内容传递到函数中时,您允许函数修改引用引用的变量。但是,当您通过值传递时,函数会创建您发送的数据的自己的局部变量副本。对该局部变量的所有更改都只在函数的作用域内。
这比说的要好。看到
int main()
{
int a = 5;
int& ra = a;
std::cout << a; // Ouput: 5
plus(&a);
std::cout << a; // Ouput: 6
return 0;
}
void plus (int& num)
{
num++;
std::cout << num; // Ouput: 6
}
请注意,当我们通过引用传递时,main()
中的a
发生了更改。如果我们只是通过价值,就会发生这种情况:
int main()
{
int a = 5;
int& ra = a;
std::cout << a; // Ouput: 5
plus(a);
std::cout << a; // Ouput: 5
return 0;
}
void plus (int num)
{
num++;
std::cout << num; // Ouput: 6
}
传递值
C总是传递参数by value
:每个参数的值的副本被传递给函数;函数无法修改传递给它的实际参数。
在您的程序中,函数void addThree (int num1, int& num2, int& num3)
有一个按值传递的参数int num1
。
也就是说,当调用函数时,在addThree
函数堆栈帧中分配一些字节的内存,并且在调用时的值,即Form语句addThree(7, num2, num3);
值7
在int num1
的该内存中传递。
其中as,在pass by reference
中,没有内存分配给变量num2 & num3
,它们将指向与main()
函数堆栈帧中的int num2 & num2
相同的地址。
根据您的计划,
x = num1 + 3; // here num 1 is form function addThree () so num1 = 7 and so 7+3 = 10
y = num2 + 3; // here num2 has same address as in the main's num2 so num2= 50, 50+3 = 53
z = num3 + 3;// here num3 has same address as in the main's num3 so num3= 50, 80+3 = 83
希望这能帮助你理解By reference and By value
的概念
您的addThree函数的形式为"addThree(int num1,int&num2,int&aamp;num3)"是正确的,但只需设置这样的参数,它就可以通过引用传递num1和其他函数的值。
"int num1"的意思是"创建一个新的int,给它num1的值,然后将其发送给函数",而"int&num1"意味着这是我的num1,但无论你在函数内部对它做什么,也会在函数外部更改它。
所以你不需要输入7,你可以说addThree(num1,num2,num3),你是对的,试着运行这个版本的代码:
#include <iostream>
using namespace std;
int main() {
void addThree(int num1, int& num2, int& num3);
int num1 = 30;
int num2 = 50;
int num3 = 80;
addThree(num1, num2, num3);
cout << "now, back in main num1 is: " << num1 << endl;
cout << "now, back in main num2 is: " << num2 << endl;
cout << "now, back in main num3 is: " << num3 << endl;
int pause; //just to keep the window open
cin >> pause; //just to keep the window open
return 0;
}
void addThree(int num1, int& num2, int& num3) {
num1 = num1 + 3;
num2 = num2 + 3;
num3 = num3 + 3;
cout << "inside function, num1 is: " << num1 << endl;
cout << "inside function, num2 is: " << num2 << endl;
cout << "inside function, num3 is: " << num3 << endl;
}
请注意,通过值传递的num1(由于使用了"int num1"而不是"int&num1")是该函数唯一没有持久影响的int。
- 将const引用参数初始化为默认参数会导致悬空引用吗
- 通过常量引用传递参数的矩阵模板类
- 非类型指针和引用模板参数,以及在编译时如何/为什么解析它们.c++
- 何时应通过引用传递矢量参数而不是按值传递矢量参数?
- 具有常量引用参数的函数模板专用化
- 作为参数引用
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 如何传递带有通过引用传递的结构参数的函数?
- 将输入参数作为右值引用传递?
- 使用引用与指针将数组作为参数传递
- 根据模板参数引用不同基类的函数
- 默认情况下C++数组作为参数引用
- 对指针C++的参数引用
- 作为非类型模板参数引用
- 重用返回语句或按参数引用返回
- 将参数引用传递给函数C++
- 从另一个参数引用的模板可变参数
- 成员变量多态性和参数引用
- 按参数引用或按参数引用之间的差异
- 无法实例化抽象类:为什么模板参数(引用)导致这种情况