shared_ptr是一个最像指针的“智能指针”,是boost.smart_ptr库中最有价值、最重要的组成部分,也是最有用的。
shared_ptr实现的是引用计数的智能指针,可以被自由拷贝和赋值,在任意的地方共享它,当没有代码使用(引用计数为0)它时才删除被包装的动态分配的对象。shared_ptr也可以安全地放到标准容器中。
示例1:
#include <boost/smart_ptr.hpp>
#include <iostream>
#include <cassert>
using namespace boost...
scoped_ptr不允许拷贝、赋值(因为scoped_ptr同时把拷贝构造函数和赋值操作符都声明为私有的,禁止对智能指针的复制操作),只能在scoped_ptr被声明的作用域使用。除了*和->外也没有定义其他的操作符(不能对scoped_ptr进行++或者--等指针算术操作)。
示例如下:
#include <boost/smart_ptr.hpp>
#include <iostream>
#include <string>
using namespace boost;
using namespace std;
int main()...
timer库的代码摘要如下:
class timer
{
public:
timer() { _start_time = std::clock(); }
void restart() { _start_time = std::clock(); }
double elapsed() const
{ return double(std::clock() - _start_time) / CLOCKS_PER_SEC; }
double elapsed_min() const
{ return double(1) / double(CLOCKS_P...