2012年2月22日

Call By Address 與 Call By Reference In C++

#include
using namespace std;
void something(int *b)
{
*b += 100;
return;
}
void some2(int &b)
{
b+=100;
}
int main()
{
int a = 5;
int *x=&a;
cout << (*x) << endl;
something(x); // b 指到 x 且x指到a的記憶體位址
cout<<*x<
something(&a); // b 指到 a 的記憶體位置;
cout << a << endl; //call by address
some2(a); // int &b = a; 與 call by address 差別在於一個需要每次都打&

沒有留言:

張貼留言