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;


}

posted @ 2013-07-06 12:36  Predator  阅读(780)  评论(0编辑  收藏  举报