實際上是一個對象
#include "stdafx.h"#include<iostream>using namespace std;void ValueTransfer(int a, int b){ int temp = a; a = b; b = temp; cout << a << b << endl; cout << "形參a與b的地址為/n"; cout << &a << ' ' << &b << endl;}void PointerTransfer(int *a, int *b){ int temp = *a; *a = *b; *b = temp; cout << *a << *b << endl; cout << "形參a與b的地址為/n"; cout << &a << ' ' << &b << endl;}void QuoteTransfer(int &a, int&b){ int temp = a; a = b; b = temp; cout << a << b << endl; cout << "形參a與b的地址為/n"; cout << &a << ' ' << &b << endl;}int main(){ int x = 3; int y = 4; //value transfer cout << "值傳遞/n"; ValueTransfer(x, y); cout << "實參x與y地址為/n"; cout << &x << ' ' << &y << endl; cout << "傳遞后實參的值為:/n"; cout << x << y << endl << endl; //PointerTransfer cout << "指針傳遞/n"; PointerTransfer(&x, &y); cout << "實參x與y地址為/n"; cout << &x << ' ' << &y << endl; cout << "傳遞后實參的值為:/n"; cout << x << y << endl << endl; //QuoteTransfer cout << "引用傳遞/n"; QuoteTransfer(x, y); cout << "實參x與y地址為/n"; cout << &x << ' ' << &y << endl; cout << "傳遞后實參的值為:/n"; cout << x << y << endl << endl; return 0;}結果:
新聞熱點
疑難解答