用指针传递也是可以的,也可以改变实参的值,但是不如引用传递高效!
template <typename T>
void swappNum(T *a,T *b)
{
T temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int x = 10;
int y = 20;
swappNum<int>(&x,&y);
cout << "x = " << x << endl;
cout << "y = " << y << endl;
return 0;
}
template <typename T>
void swappNum(T *a,T *b)
{
T temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int x = 10;
int y = 20;
swappNum<int>(&x,&y);
cout << "x = " << x << endl;
cout << "y = " << y << endl;
return 0;
}