C++ 为什么这个程序显示3个输出,为什么b会覆盖a? #包括“stdafx.h” #包括 使用名称空间std; 无效(内部和a、内部和b) { a=10; b=20; cout

C++ 为什么这个程序显示3个输出,为什么b会覆盖a? #包括“stdafx.h” #包括 使用名称空间std; 无效(内部和a、内部和b) { a=10; b=20; cout,c++,function,variables,overriding,C++,Function,Variables,Overriding,您正在将z的引用传递给silly()函数 在silly()中更改a或b的值(都引用z)时,它会更改z的值 因此,在这一行: #include "stdafx.h" #include <iostream> using namespace std; void silly(int & a, int & b) { a=10; b=20; cout << a << "" << b << endl; }

您正在将z的引用传递给silly()函数

在silly()中更改a或b的值(都引用z)时,它会更改z的值

因此,在这一行:

#include "stdafx.h"
#include <iostream>

using namespace std;

void silly(int & a, int & b)
{
    a=10;
    b=20;
    cout << a << "" << b << endl;
}


void main()
{
    int z=30;
    silly(z,z);
    cout << z << endl;
    system("Pause");

}

cout有3个输出,因为你输出了a、b和z,3件事。b覆盖了a,因为你是通过引用传递的。这更有意义,只有一个问题,为什么有3个输出?前两个输出来自dully()中的cout.第三个输出来自主屏幕。哦,天哪,这更有意义!非常感谢
  cout << a << "" << b << endl;