#include<iostream>
using namespace std;
class Person {
public:
Person()
{
cout << "Person的无参构造函数的调用" << endl;
}
Person(int a,int height)
{
age = a;
m_height = &height;
cout << "Person的有参构造函数的调用" << endl;
}
Person(const Person &p)
{
age = p.age;
m_height = p.m_height;
cout << "Person的拷贝构造函数的调用" << endl;
}
~Person()
{
cout << "Person的析构函数调用" << endl;
}
int age;
int *m_height;
};
void test()
{
Person p1(18,172);
Person p2(p1);
cout << p2.age<<" "<< *p2.m_height << endl;
}
int main()
{
test();
system("pause");
return 0;
}
为什么*p2.m_height输出是乱码,我里面是把height的地址给了指针m_height了呀