IN, OUT, INOUT Parameters
IN, OUT, INOUT Parameters
请解释程序的执行情况,以及为什么它会生成如此特定的输出。
输出为
6 10 20
6 10 8
2 2 14
我的猜测是,这是由于IN、OUT、INOUT参数造成的,但我并不真正理解
#include <iostream>
using namespace std;
void sunny(int&, int);
void cloudy(int, int&);
int temp;
int main()
{
int num1 = 6;
int num2 = 10;
temp = 20;
cout << num1 << " " << num2 << " " << temp << endl;
sunny(num1, num2);
cout << num1 << " " << num2 << " " << temp << endl;
cloudy(num1, num2);
cout << num1 << " " << num2 << " " << temp << endl;
}
void sunny(int& a, int b)
{
int w;
temp = (a + b) / 2;
w = a / temp;
b = a + w;
a = temp - b;
}
void cloudy(int u, int& v)
{
temp = 2 * u + v;
v = u;
u = v - temp;
}
实际上输出是:
6 10 20
2 10 8
2 2 14
我们可以看到这样的步骤:
阳光呼叫:
temp = (6 + 10)/2 = 8
w = 6/8 = 0 //This is because w is declared int, and int(6/8) = 0
b = 6 + 0 = 10
a = 8 - 6 = 2
因此,由于引用参数的原因,num1现在是2,而temp由于其全局而更改为8
多云呼叫:
temp = 2 * 2 + 10 = 14
v = 2
u = 2 - 14 = -12 //This variable is doing nothing
因此num2现在是2,原因和以前一样,temp是14
相关文章:
- IN, OUT, INOUT Parameters
- 从 std::ostream 重载 << 运算符时,为什么编译器会给出"too many parameters for this operator function"错误?
- Rcpp 'candidate function has different number of parameters (expected 0 but has 1)'
- 未来C++标准支持"Declaring type template parameters with auto"
- SQLHANDLE-Parameters:函数不接受x个参数(但实际上确实如此)
- SWIG INOUT types (C++ to Python)
- Parameters of OpenCV Mat::Mul
- 当类型和参数长度可能不同时,如何将'function with parameters'作为参数传递?
- OpenCV: Orb parameters
- parameters for LsaICryptUnprotectData
- C++ fstream getline parameters
- QtCreator 4 and CMake parameters
- int const函数(参数)、int函数(const参数)和int函数(parameters)const之间的区别是什
- 未定义范围 c++ 需要 int inout
- C# String.Format with Parameters 标准等效于 C++
- 我不明白为什么我的输出是 1,1 如何将其更改为 "type name" 和 "parameters" ?蒂亚
- C++ rectangle() parameters
- 在 c++ 中,你有像 c# 中那样'out parameters'吗?
- 如何包装"expanded variadic template parameters"?
- 输入具有 C "boolean"返回类型和"no parameters"的功能