Permalink
Browse files

智能指针小试

  • Loading branch information...
1 parent 1a07fd1 commit 3677b6a3a417d8db91b5dd0f6074743f42ead159 = committed Dec 30, 2013
Showing with 42 additions and 0 deletions.
  1. +42 −0 shared_ptr.cpp
View
@@ -0,0 +1,42 @@
+
+
+#include <iostream>
+#include <string>
+#include <boost/smart_ptr.hpp>
+
+class TC {
+public:
+ TC(std::string strParam):str(strParam){
+ std::cout<<"TC constructor"<<std::endl;
+ }
+ ~TC(){
+ std::cout<<"~TC destructor"<<std::endl;
+ }
+ void print_str(int flag=0){
+ std::cout<<flag<<" "<<str<<std::endl;
+ }
+
+private:
+ std::string str;
+
+};
+
+
+boost::shared_ptr<TC> func_test(){
+ boost::shared_ptr<TC> test_pc(new TC("hello world"));
+ test_pc->print_str(1);
+ std::cout<<"use_count = "<<test_pc.use_count()<<std::endl;
+ return test_pc;
+}
+
+int main(){
+ boost::shared_ptr<TC> test_pc2 = func_test();
+ test_pc2->print_str(2);
+ std::cout<<"test_pc2 use_count = "<<test_pc2.use_count()<<std::endl;
+
+ boost::shared_ptr<TC> test_pc3 = test_pc2;
+ test_pc3->print_str(2);
+ std::cout<<"test_pc3 use_count = "<<test_pc3.use_count()<<std::endl;
+
+ return 0;
+}

0 comments on commit 3677b6a

Please sign in to comment.