传指针和传引用两种方式的不同
传指针是传的一个指针的复制,可以改变指针指向的地址的值,不能改变指针指向的地址。
传引用是传的指针的地址,指针指向的地址和指针指向的地址的值都可以改变。
#include "iostream"
using namespace std;
void test_ptr(int* p){
cout<< *p<< endl;
*p = 5;
cout<< *p<< endl;
int * pp = new int();
*pp = 50;
p = pp;
cout<<*p<<endl;
}
void test_pptr(int& p){
cout<< p<< endl;
p = 5;
cout<< p<< endl;
int * pp = new int();
*pp = 50;
p = *pp;
}
int main(void) {
int* p = new int();
*p = 11;
test_ptr(p);
cout<<*p<<endl;
test_pptr(*p);
cout<<*p<<endl;
delete p;
return 0;
}