#include<iostream>
#include<string>
using namespace std;
class Person {
public:
Person() {
cout << "无参构造函数的调用!" << endl;
}
Person(string name, int age) {
this->m_name = name;
this->m_age = new int(age);
cout << "有参构造函数的调用!" << endl;
}
Person(const Person& p) {
this->m_age = p.m_age;
this->m_name = p.m_name;
cout << "拷贝构造函数的调用!" << endl;
}
~Person() {
if (m_age == nullptr) delete m_age;
cout << "析构函数的调用!" << endl;
}
string m_name;
int* m_age;
};
void test() {
Person p1("李文", 20);
Person p2(p1);
}
int main() {
test();
system("pause");
return 0;
}
偶然发现这样一个问题,是怎么回事?