#include<iostream>
using namespace std;
void fun1(int *p1)
{
p1=new int(10);
cout<<"*p1="<<*p1<<endl;
}
void fun2(int * & p2)
{
p2=new int(5);
cout<<"*p2="<<*p2<<endl;
}
void fun3(int *p1)
{
*p1=122;
cout<<"fun3:*p1="<<*p1<<endl;
}
void main()
{
int *p=new int(1);
cout<<"main:*p="<<*p<<endl;
fun3(p);
cout<<"main:*p="<<*p<<endl;
fun1(p);
cout<<"main After:fun1:*p="<<*p<<endl;
fun2(p);
cout<<"main After:fun2:*p="<<*p<<endl;
}
以上代码,运行后结果如下图:
fun1()函数里的p1指针,有发生传值调用时的拷贝!最耐人寻味的是fun2!!!