boost 程序库完全开发指南_ch3_memory_manager
ch3的代码块
好像还真的不用编译,只需要把代码嵌入即可
#include <iostream> #include <memory> #include <vector> using namespace std; #include <boost/smart_ptr.hpp> #include <boost/make_shared.hpp> #include <boost/enable_shared_from_this.hpp> using namespace boost; #if 0 struct posix_file //一个示范性质的文件类 { posix_file(const char * file_name) //构造函数打开文件 {cout << "open file:" << file_name << endl; } ~posix_file() //析构函数关闭文件 {cout << "close file" << endl; } }; #endif #if 0 class shared //一个拥有shared_ptr的类 { private: shared_ptr<int> p; //shared_ptr成员变量 public: shared(shared_ptr<int> p_):p(p_){} //构造函数初始化shared_ptr void print() //输出shared_ptr的 引用计数和指向的值 { cout << "count:" << p.use_count() << "v =" <<*p << endl; } }; void print_func(shared_ptr<int> p) //使用shared_ptr作为函数参数 { //同样输出shared_ptr的引用计数和指向的值 cout << "count:" << p.use_count() << " v=" <<*p << endl; } void print_func_ref(shared_ptr<int>& p) //使用shared_ptr作为函数参数 { //同样输出shared_ptr的引用计数和指向的值 cout << "count:" << p.use_count() << " v=" <<*p << endl; } #endif #if 0 struct XX{ XX(string str) { cout << "con xx " << str.c_str()<<endl; //cout << "con XX" <<endl; } XX() { cout << "con XX" <<endl; } ~XX() { cout << "des XX" <<endl; } }; #endif #if 0 class abstract { public: virtual void f() = 0; virtual void g() = 0; protected: virtual ~abstract(){ cout << "des" << endl; } }; class impl: public abstract { public: virtual void f() { cout << "f()" << endl; } virtual void g() { cout << "g()" << endl; } }; shared_ptr<abstract> create() { return shared_ptr<abstract>(new impl); } #endif #if 0 class sample { private: class impl; //不完整的内部类声明 shared_ptr<impl> p; //shared_ptr成员变量 public: sample(); //构造函数 void print(); //提供给外界的接口 }; //在sample的cpp中完整定义impl类和其他功能: class sample::impl //内部类的实现 { public: void print() { cout << "impl print" << endl;} }; sample::sample():p(new impl){} //构造函数初始化shared_ptr void sample::print() //调用pimpl实现print() { p->print();} #endif #if 0 struct my_socket{ my_socket(){cout << "con" << endl; }; ~my_socket(){cout << "des" << endl;} }; my_socket* open_socket() { cout << "open socket" <<endl; return new my_socket; } void close_socket(my_socket* t) { cout <<"close socket" <<endl; delete t; } #endif class self_shared : public enable_shared_from_this<self_shared> { public: self_shared(int n ):x(n){cout <<"con"<<endl;} ~self_shared(){ cout <<"des" <<endl;} int x; void print() { cout << "self_shared " << x << endl; } }; int main() { shared_ptr<self_shared> sp = make_shared<self_shared>(314); sp->print(); shared_ptr<self_shared> p = sp->shared_from_this(); //self_shared* p = sp->shared_from_this(); p->x = 100; p->print(); #if 0 my_socket* s= open_socket(); shared_ptr<my_socket> p(s, &close_socket); #endif #if 0 shared_ptr<abstract> p = create(); p->f(); p->g(); //abstract* ab = p.get(); //delete ab; impl* p2 = (impl*)(p.get()); delete p2; _exit(0); #endif #if 0 sample s; s.print(); #endif #if 0 typedef vector<shared_ptr<XX> > vs; vs v(10); int i = 0; cout <<"before loop" <<endl; for (vs::iterator pos = v.begin(); pos != v.end(); ++pos, ++i) { //*pos = make_shared<XX>(); *pos = make_shared<XX>("abc"); } cout <<"after loop" <<endl; #endif #if 0 typedef vector<shared_ptr<int> > vs; //一个持 有shared_ptr的标准容器类型 vs v(10); //声明一 个拥有10个元素的容器,元素被 //初始化为空指针 int i = 0; for ( vs::iterator pos = v.begin(); pos != v.end(); ++pos) { (*pos) = make_shared<int>(++i); //使用工厂函数赋值 cout << *(*pos) << ", "; //输出值 } cout << endl; shared_ptr<int> p = v[9]; *p = 100; cout << *v[9] << endl; i = 0; for ( vs::iterator pos = v.begin(); pos != v.end(); ++pos) { // (*pos) = make_shared<int>(++i); //使用工厂函数赋值 cout << *(*pos) << ", "; //输出值 } #endif #if 0 shared_ptr<string> sp = make_shared<string>("hello shared_ptr"); cout << sp->c_str() <<endl; //cout << (*sp) <<endl; #endif #if 0 shared_ptr<int> p(new int(100)); shared s1(p), s2(p); //构造两个自定义类 s1.print(); s2.print(); *p = 20; //修改shared_ptr所指的值 print_func(p); *p = 40; //修改shared_ptr所指的值 print_func_ref(p); s1.print(); #endif #if 0 scoped_ptr<int> p(new int); //一个int指 针的scoped_ptr if (p) //在bool语 境中测试指针是否有效 { *p = 100; //可以像普通 指针一样使用解引用操作符* cout << *p << endl; } p.reset(); //reset()置 空scoped_ptr,仅仅是演示 assert(p == 0); //p不持有任何指针 if (!p) //在bool语 境中测试,可以用!操作符 { cout << "scoped_ptr == null" << endl; } //文件类的scoped_ptr, //将在离开作用域时自动析构,从而关闭文件释放资源 scoped_ptr<posix_file> fp(new posix_file("/tmp/a.txt")); #endif #if 0 auto_ptr<int> ap(new int(10)); //一个int自动指针 scoped_ptr<int> sp(ap); //从auto_ptr获得原始指针 assert(ap.get() == 0); //原auto_ptr不再拥有指针 ap.reset(new int(20)); //auto_ptr拥有新的指针 cout << *ap << "," << *sp << endl; auto_ptr<int> ap2; ap2 = ap; //ap2从ap获 得原始指针,发生所有权转移 assert(ap.get() == 0); //ap不再拥有指针 scoped_ptr<int> sp2(new int(30)); //另一个scoped_ptr // sp2 = sp; //赋值操作, 无法通过编译!! sp.swap(sp2); cout << *sp << *sp2 <<endl; #endif #if 0 //int* arr = new int[100]; scoped_array<int> sa(new int[100]); fill_n(&sa[0], 100, 5); //fill_n(sa, 100, 5); //fill_n(arr, 100, 5); sa[10] = sa[20] + sa[30]; cout << sa[10]; #endif #if 0 shared_ptr<int> sp(new int(10)); //一个指向整数的shared_ptr assert(sp.unique()); //现在shared_ptr 是指针的唯一持有者 shared_ptr<int> sp2 = sp; //第二个shared_ptr, 拷贝构造函数 //两个shared_ptr相等,指向同一个对象,引用计数为2 assert(sp == sp2 && sp.use_count() == 2); *sp2 = 100; //使用解引用操作符修改被指对象 assert(*sp == 100); //另一个shared_ptr也同时被修改 cout << *sp << *sp2 << endl; sp.reset(); //停止shared_ptr的使用 assert(!sp); //sp不再持有任何指针(空指针) #endif } #if 0 void func(const auto_ptr<int>& pInt) { cout << *pInt<<endl; } class X { public: X(){cout <<"construct"<<endl;} ~ X(){cout <<"destructor"<<endl;} }; int main() { #if 0 auto_ptr<int> a(new int(100)); func(a); #endif //error use begin X* x = new X; auto_ptr<X> p1(x); delete x; //destructor 1 //error use begin return 0; } //destructor 2 #if 0 //normal use int main() { cout <<"before scope"<<endl; { auto_ptr<X> p1(new X); cout <<"I will exit the scope" <<endl; } cout <<"after scope"<<endl; cout <<"------------------------------"<<endl; cout <<"before scope"<<endl; { auto_ptr<X> p1(new X); delete p1;; cout <<"I will exit the scope" <<endl; } cout <<"after scope"<<endl; return 0; } #endif //normal use #endif