我有以下代码:
该程序输出:
该程序实际上交换了数值!为什么?因为
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <conio.h>
#include <cstring>
#include <iomanip>
void swap(long a, long b)
{
long temp;
temp=a;
a=b;
b=temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x = 5, y = 3;
cout << x ;
cout << y << endl;
swap(x, y);
cout << x ;
cout << y << endl;
getch();
return 0;
}
该程序输出:
5 3
3 5
该程序实际上交换了数值!为什么?因为
swap()
的参数不是指针或引用。
(我正在使用VS 2005)