C++ 智能指针生成工厂make_shared

#include <string>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost;
using namespace std;

class Person
{
public:
Person(){}
Person(string name, int age){this->m_strName = name; this->m_nAge = age;}
~Person(){}
void DisplayMsg(){cout << "姓名:" << this->m_strName << " " << "年龄:" << this->m_nAge << endl;}

void ChangeInfo(string name, int age){this->m_strName = name; this->m_nAge = age;}
private:
string m_strName;
int m_nAge;

};

class Share
{
public:
Share(){}
Share(boost::shared_ptr<Person> spPerson){ this->m_spPeson = spPerson;}
~Share(){}
void ShowInfo(){
cout << "use_count:" << m_spPeson.use_count() << endl;;
m_spPeson->DisplayMsg();}

private:
boost::shared_ptr<Person> m_spPeson;

};
void main()
{
boost::shared_ptr<Person> person = boost::make_shared<Person>(Person("甘全福", 25)); //利用工厂生成share_ptr<Person> person智能指针
cout << " use_count" << person.use_count() << endl ;

{
Share myShare(person);
myShare.ShowInfo();
}

{
Share otherPerson(person);
otherPerson.ShowInfo();
}

cout << " use_count" << person.use_count() << endl ;
/*person->ChangeInfo("陈星", 88);
myShare.ShowInfo();
otherPerson.ShowInfo();
*/
int wait;
cin >> wait;

}

原文链接: https://www.cnblogs.com/ganquanfu2008/p/3175016.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ 智能指针生成工厂make_shared

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/94814

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午2:49
下一篇 2023年2月10日 上午2:49

相关推荐