次のコードがあります。
#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を使用しています)