//输出5,3,完成swap
//*************solution1*************
#include<iostream>
using namespace std;
class A
{
public:
int x;
int getX() { return x; }
private:
};
void swap(A &a,A &b)
{
int tmp = a.x;
a.x = b.x;
b.x = tmp;
}
int main()
{
A a, b;
a.x = 3;
b.x = 5;
swap(a,b);
cout << a.getX() << " "<<b.getX();
return 0;
}
//*************solution2*************
#include<iostream>
using namespace std;
void swap(int * &a,int * &b)
{
int *tmp = a;
a=b;
b = tmp;
}
int main()
{
int a=3, b=5;
int * pa = &a;
int * pb = &b;
swap(pa,pb);
cout << *pa << " " << *pb << endl;
cout << a << " "<<b;
return 0;
}
//reference as return value of fun
//引用函数的返回值
#include<iostream>
using namespace std;
int & getelement(int *arr, int i)
{
return arr[i];
}
int main()
{
int a[] = { 1,2,3 };
getelement(a, 1)=10;
cout << a[1];
return 0;
}
//array intilization using dynamic memory
#include<iostream>
using namespace std;
int main()
{
int * a[] = { 0,0,new int[6],new int[6] };
* a[2] = 123;
a[3][5] = 456;
if (!a[0])
{
cout << *a[2] <<" "<< a[3][5] << endl;
}
return 0;
}