C++ boost有关智能指针与普通指针的转换
#include<iostream>
#include "common.h"
#include<string>
#include <ctype.h>
#include "Calendar.h"
#include <iostream>
#include<boost/date_time.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
using namespace boost::posix_time;
using namespace boost::gregorian;
using namespace std;
struct tagPerson
{
public:
string name;
int age;
};
int main(int argc)
{
boost::shared_ptr<tagPerson> myPerson = boost::make_shared<tagPerson>();
myPerson->name = "ganquanfu";
myPerson->age = 29;
cout << myPerson->name << " " << myPerson->age << endl;
tagPerson *ptagPerson = myPerson.get();
cout << "普通指针获取" << endl;
cout << ptagPerson->name << " " << ptagPerson->age << endl;
cout << "智能指针输出" << endl;
cout << myPerson->name << " " << myPerson->age << endl;
cout << myPerson.use_count() << endl;
int wait;
cin >> wait;
return 0;
}
boost::shared_ptr<tagPerson> myPerson = boost::make_shared<tagPerson>();
myPerson->name = "ganquanfu";
myPerson->age = 29;
cout << myPerson->name << " " << myPerson->age << endl;
tagPerson *ptagPerson = myPerson.get();
cout << "普通指针获取" << endl;
cout << ptagPerson->name << " " << ptagPerson->age << endl;
delete ptagPerson; //释放了指针指向的内存空间,后面用智能指针访问的时候,就会报错;
cout << "智能指针输出" << endl;
cout << myPerson->name << " " << myPerson->age << endl;
cout << myPerson.use_count() << endl;