#include <iostream>
#include <memory>
using namespace std;
class Person
{
public:
Person()
{
cout << "Person()构造" << endl;
}
~Person()
{
cout << "~Person()析构" << endl;
}
Person(const Person &p)
{
cout << "Person()拷贝构造" << endl;
this->name = p.name;
this->age = p.age;
}
const Person& getPerson()
{
return *this;
}
string name;
int age;
};
int main()
{
std::shared_ptr<Person> p1(new Person);
cout << p1.use_count() << endl;
cout << p1.get() << endl;
std::shared_ptr<Person> p2(p1);
cout << p2.use_count() << endl;
cout << p2 << endl;
p2.get()->name = "furong";
cout << p1.get()->name << endl;
Person p3(*p1);
cout << p3.name << endl;
return 0;
}
$ ./a.out
Person()构造
1
0x189ec20
2
0x189ec20
furong
Person()拷贝构造
furong
~Person()析构
~Person()析构