指针可以接收空参数,而引用则不能。如果要传递“无对象”,则只能使用指针。
通过指针显式传递允许我们查看对象是通过引用传递还是在调用站点传递值。
这些是通过指针传递和通过引用传递的简单示例-
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
   int c = *a;
   *a= *b;
   *b = c;
}
int main() {
   int m =7 , n = 6;
   cout << "Before Swap\n";
   cout << "m = " << m << " n = " << n << "\n";
   swap(&m, &n);
   cout << "After Swap by pass by pointer\n";
   cout << "m = " << m << " n = " << n << "\n";
}输出结果
Before Swap m = 7 n = 6 After Swap by pass by pointer m = 6 n = 7
#include <iostream>
using namespace std;
void swap(int& a, int& b) {
   int c = a;
   a= b;
   b = c;
}
int main() {
   int m =7 , n = 6;
   cout << "Before Swap\n";
   cout << "m = " << m << " n = " << n << "\n";
   swap(m, n);
   cout << "After Swap by pass by reference\n";
   cout << "m = " << m << " n = " << n << "\n";
}输出结果
Before Swap m = 7 n = 6 After Swap by pass by reference m = 6 n = 7