hjk41的日志

Avatar

More Effective C++ 28: Smart pointers

1. auto_ptr is included in <memory>.
2. assignment is done by ownership transfer


auto_ptr<string> p1(new string("hello"));  // p1 points to the string
auto_ptr<string> p2=p1;  // ownership transferred to p2

assert(p1.get()==NULL);  // now p1 points to NULL
cout<<*p2<<endl;  // and p2 points to the string

评论已关闭