如果要使用boost的shared_ptr要知道与VS自带的那个shared_ptr避免冲突。
因为在VS的include目录下有一个全局的shared_ptr,所以应该像如下这样正规的使用boost的shared_ptr
boost的所有智能指针都在smart_ptr.hpp头文件中。
#include <vector> #include <boost/smart_ptr.hpp> #include <iostream> using namespace std; class test; typedef boost::shared_ptr<test> test_ptr ; class test { public: test(){cout<<"a test object created"<<endl;} ~test(){cout<<"a test object destroyed"<<endl;} void print(void) const {cout<<"test print"<<endl;} }; int main() { test_ptr pt(new test); vector<test_ptr> test_ptr_array; for (int i=0;i<10;i++) { test_ptr_array.push_back(pt); } for (int i=0;i<10;i++) { test_ptr_array[i]->print(); } return 0; }
原文:http://blog.csdn.net/calmreason/article/details/20306073