源程序:
#include <iostream>
using namespace std;
class pointer
{
public:
int a;
int *p;
pointer()
{
a = 100;
p = new int(10);
}
pointer(const pointer &tempp)
{
if (this != &tempp)
{
a = tempp.a;
p = tempp.p;
}
}
};
int main()
{
pointer p1;
pointer p2(p1);
cout << p1.a << ", " << *p1.p << ", " << (p1.p == p2.p) << endl;
*p1.p = 20;
p2.a = 300;
cout << (p1.a == p2.a) << ", " << *p1.p << ", " << (p1.p == p2.p) << endl;
system("pause");
return 1;
}
运行结果: