码迷,mamicode.com
首页 > 其他好文 > 详细

boost smart pointer

时间:2019-05-29 17:52:27      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:sam   false   last   for   can   div   nsf   example   delete   

1. boost::scoped_ptr is a smart pointer that is the sole owner of a dynamically allocated object and cannot be copied or moved.

#include <boost/scoped_ptr.hpp>
#include <iostream>

int main() {
  boost::scoped_ptr<int> p(new int(1));
  std::cout << *p << std::endl;  p指针地址
  p.reset(new int(2));  reset()释放之前的对象,重新设置新的对象
  std::cout << *p.get() << std::endl;  get()获取指针地址
  p.reset();
  std::cout << std::boolalpha << static_cast<bool>(p) << std::endl;  返回p的bool值,此时为false
  return 0;
}

A smart pointer of type boost::scoped_ptr can‘t transfer ownership of an object. Once initialized an address, the dynamically allocated object is released when

the destructor is executed or when the member function reset() is called.

 

2. boost::scoped_array is used like boost::scoped_ptr. The crucial difference is that the destructor of boost::scoped_array uses the operator delete[] to release the contained object. Because this operator only applies to arrays, a boost::scoped_array must be initialized with the address of a dynamically allocated array.

#include <boost/scoped_array.hpp>

int main() {
  boost::scoped_array<int> p(new int[2]);
  *p.get() = 1;
  p[1] = 2;
  p.reset(new int[3]);
  return 0;
}

 

3.  The smart pointer boost::shared_ptr is similar to boost::scoped_ptr. The key difference is that boost::shared_ptr is not necessarily the exclusive owner of an object. Onwership can be shared with other smart pointers of type boost::shared_ptr. In such a case, the shared object is not released until the last copy of the shared pointer referencing the object is destroyed. The smart pointer can be copied.

#include <boost/shared_ptr.hpp>
#include <iostream>

int main() {
  boost::shared_ptr<int> p1(new int(1));
  std::cout << *p1 << std::endl;
  boost::shared_ptr<int> p2(p1);
  p1.reset(new int(2));
  std::cout << *p1.get() << std::endl;
  p1.reset();
  std::cout << std::boolalpha << static_cast<bool>(p2) << std::endl;
  return 0;
}

输出为:

1

2

true

When reset() is called on p1, a new int object is anchored in p1. This doesn‘t mean that the existing int object is destroyed. Since it is also anchored in p2, it continues to exist. boost::shared_ptr uses a reference counter internally. Only when boost::shared_ptr detects that the last copy of the smart pointer has been destroyed is the contained object released with delete.

 

4. boost::shared_array  calls delete[] in the destructor, this smart pointer can be used for arrays.

#include <boost/shared_array.hpp>
#include <iostream>

int main() {
  boost::shared_array<int> p1(new int[1]);
  {
    boost::shared_array<int> p2(p1);
    p2[0] = 1;
  }
  std::cout << p1[0] << std::endl;
  return 0;
}

the smart pointers p1 and p2 share ownership of the dynamically allocated int array. When the array in p2 is accessed with operator[] to store the number 1, the same array is accessed with p1. Thus, the example writes 1 to standard output.

 

5. boost::weak_ptr only make sense if used in conjunction with boost::shared_ptr.

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <thread>
#include <functional>
#inclue <iostream>

void reset(boost::shared_ptr<int>& sh) {
  sh.reset();
}

void print(boost::weak_ptr<int>& w) {
  boost::shared_ptr<int> sh = w.lock();
  if (sh) {
    std::cout << *sh << std::endl;
  }
}

int main() {
  boost::shared_ptr<int> sh(new int(99));
  boost::weak_ptr<int> w(sh);
  std::thread t1(reset, std:ref(sh));
  std::thread t2(print, std::ref(w));
  t1.join();
  t2.join();
  return 0;
}

boost::weak_ptr must be initialized with a boost::shared_ptr. Its most important member function is lock(). lock() returns a boost::shared_ptr that shares ownership with the shared pointer used to initialize the weak pointer. In case the shared pointer is empty, the returned pointer will be empty as well.

 

boost smart pointer

标签:sam   false   last   for   can   div   nsf   example   delete   

原文地址:https://www.cnblogs.com/sssblog/p/10945129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!