auto_ptr、shared_ptr、weak_ptr、scoped_ptr用法小结
智能指针,与引用计数相关
auto_ptr: 主要为异常安全设计的,在程序正常退出或者异常终止,会调用类的析构函数,释放资源。
复制\赋值是损坏性的操作,所以不能绑定到数组或者变量指针,也不能将auto_ptr对象存储在容器中。
auto_ptr<int> a(new int(10));
auto_ptr<int> b;
b.reset(new int(5));
a = b;
//先解除a绑定的对象
//将a指向b
//b最后为未绑定状态
if (a.get()) //来判断是否初始化auto_ptr实例
shared_ptr:
构造函数
std::shared_ptr<int>p5 (new int, [](int* p){ delete p;}, std::allocator<int>());
可以指定deleter
boost::static_visitor
visitor设计模式
不同的操作,使用同一接口访问、调用
visitor模式实现了不改变类本身,却改变类的行为的功能
想要分别处理各种不同类型的数据, Boost.Variant 为我们提供了一个名为 boost::apply_visitor() 的函数。
#include<boost/variant.hpp>
#include<boost/any.hpp>
#include<vector>
#include<string>
#include<iostream>
std::vector<boost::any>vector;
structoutput :
public boost::static_visitor<>
{
void operator()(double &d) const
{
vector.push_back(d);
}
void operator()(char &c) const
{
vector.push_back(c);
}
void operator()(std::string &s) const
{
vector.push_back(s);
}
};
intmain()
{
boost::variant<double, char,std::strin