一、智能指针类:std::auto_ptr
由于 auto_ptr 基于【排他所有权模式】,这意味着:两个指针(同类型)不能指向同一个资源,复制或赋值都会改变资源的所有权。
一个简单的例子1:
[cpp] view plain copy
#include <iostream>
#include <memory>
class A
{
public:
void print(){std::cout<<"A::print"<<std::endl;}
};
int main()
{
std::auto_ptr<A>pa1(new A);
pa1->print();
std::cout<<"pa1 pointer:"<<pa1.get()<<std::endl;
std::auto_ptr<A>pa2(pa1); //copy constructor
pa2->print();
std::cout<<"pa1 pointer:"<<pa1.get()<<std::endl;
std::cout<<"pa2 pointer:"<<pa2.get()<<std::endl;
return 0;
}
输出:
即即经过复制构造之后,pa1所指向资源的所有权转向了pa2,而pa1变成空,二者不能同时共享该资源。
auto_ptr 主要有两大问题:
复制和赋值会改变资源的所有权,不符合人的直觉。
在 STL 容器中无法使用auto_ptr ,因为容器内的元素必需支持可复制(copy constructable)和可赋值(assignable)。
二、C++11中新增的智能指针
新加入标准模板库(STL)的智能指针有两个:
shared_ptr:基于引用计数模型。每次有 shared_ptr 对象指向资源,引用计数器就加1;当有 shared_ptr 对象析构时,计数器减1;当计数器值为0时,被指向的资源将会被释放掉。且该类型的指针可复制和可赋值,即其可用于STL容器中。此外,shared_ptr 指针可与多态类型和不完全类型一起使用。主要缺点:无法检测出循环引用(后面会细说),如一颗树,其中既有指向孩子结点的指针又有指向父亲结点的指针,即孩子父亲相互引用。这会造成资源无法释放,从而导致内存泄露。为了 fix 这个问题,引入了另一个智能指针:weak_ptr.
weak_ptr:指向有shared_ptr 指向的资源(即其需要shared_ptr的参与,其辅助 shared_ptr 之用),但是不会导致计数。一旦计数器为0,不管此时指向资源的 weak_ptr 指针有多少,资源都会被释放,而所有的这些 weak_ptr 指针会被标记为无效状态(即 weak_ptr作为观察shared_ptr 的角色存在着,shared_ptr 不会感受到 weak_ptr 的存在)。
上一例子的shared_ptr 实现-例子2:
[cpp] view plain copy
#include <iostream>
#include <memory>
class A
{
public:
void print(){std::cout<<"A::print"<<std::endl;}
};
int main()
{
std::shared_ptr<A>sp1(new A); //namespace: std::
sp1->print();
std::cout<<"sp1 pointer:"<<sp1.get()<<std::endl;
std::shared_ptr<A>sp2(sp1); //copy constructor
sp2->print();
std::cout<<"sp1 pointer:"<<sp1.get()<<std::endl;
std::cout<<"sp2 pointer:"<<sp2.get()<<std::endl;
std::cout<<"count sp1:"<<sp1.use_count()<<std::endl; //get reference count
std::cout<<"count sp2:"<<sp2.use_count()<<std::endl;
return 0;
}
输出:
可知:sp2创建后,sp1对资源的所有权并没有被剥夺,而是sp1 和 sp2 均指向了资源,且此时资源的引用计数为2。当两个shard_ptr 指针sp1、sp2 超过其作用域时,最后一个析构的指针将会致使资源的释放(因为引用计数为0了)。
三、智能指针类:std::tr1::shared_ptr
3.1 概念
std::shared_ptr 智能指针共享所指向的资源(所有权),即几个 shared_ptr 可同时拥有一个对象,且共享一个控制块(constrol block),包含指向资源的 shared_ptr对象个数、指向资源的 weak_ptr 对象个数以及删除器(deleter:用户自定义的用于释放资源的函数,可以默认没有)。
一个空的 shared_ptr 对象不拥有任何资源和控制块。另一方面,一个 shared_ptr 初始化为一个NULL 指针和一个控制块,这有别有空的 shared_ptr。当共享的引用计数器为0时,资源释放(delete 操作符释放,或由用户提供的 删除器 释放它)。
3.2 使用
(1)创建一个 shared_ptr 对象
常见,一个 shared_ptr 对象可由以下四种对象来构造:
指向任何类型 T 的指针(包括 const T),也可为指向的资源指定删除器释放它;
另一个 shared_ptr 对象;
一个 weak_ptr 对象;
一个 auto_ptr 对象。
它们对应的构造函数如下:
[cpp] view plain copy
//1
template<class T>
explicit shared_ptr(T*);
template<class T, class D>
shared_ptr(T*, D);
//2
template<class T>
shared_ptr(const shared_ptr<T>&);
//3
template<class T>
shared_ptr(const weak_ptr<T>&);
//4
shared_ptr(const auto_ptr&);
(2)删除器(deleter)与 get_deleter函数:
get_deleter函数返回一个指针,指向shared_ptr 的删除器,如果没有提供删除器则返回0。
例子3:
[cpp] view plain copy
#include <iostream>
#include <memory>
class A
{
public:
static A* alloc()
{
A* pa = new A;
std::cout<<"a new object was created"<<std::endl;
return pa;
}
static void free(A* pa)
{
delete pa;
std::cout<<"A object was destroyed"<<std::endl;
}
};
typedef void(*deleter)(A*); //define a function type pointer to free;
int main()
{
std::shared_ptr<A> spa(A::alloc(), &A::free);//deleter: &A::free()
deleter* del = std::get_deleter<deleter>(spa);
std::cout<<"get_deleter(spa)!=0 == "<<std::boolalpha<<(del!=0)<<std::endl;
return 0;
}
输出:
(3)-> 、 * 操作符和 get 函数
shared_ptr 类重载了-> 操作符和 * 操作符,前者返回指向资源的指针;后者指向资源的引用。故无需内部指针。其原型如下:
[cpp] view plain copy
template<class T>
class shared_ptr
{
public:
T* get() const;
T& operator*()const;
T* operator->()const;
};
其中 get 函数返回指向资源的指针,基本等同于 ->操作符,且与auto_ptr 兼容。
例子4:
[cpp] view plain copy
#include <iostream>
#include <memory>
class A
{
public:
void print(){std::cout<<"A::print"<<std::endl;}
};
int main()
{
std::shared_ptr<A> sp(new A);
A* pa = sp.get();
if(pa)pa->print();
std::cout<<"-> operator: ";
sp->print();
std::cout<<"* operator: ";
(*sp).print();
return 0;
}
输出:
(4)条件操作符(bool operator)
shared_ptr 类提供了布尔操作符,允许 shared_ptr 对象用于布尔表达式去检查是否该shared_ptr对象里的指针为NULL。
例子5:
[cpp] view plain copy
#include <iostream>
#include <memory>
#include <string>
int main()
{
std::shared_ptr<std::string> sp1;
if(sp1)
{
std::cout<<"pointer in sp1 is not NULL"<<std::endl;
}
else
{
std::cout<<"pointer in sp1 is NULL"<<std::endl;
}
std::shared_ptr<std::string> sp2(new std::string("hello world"));
if(sp2)
{
std::cout<<"pointer in sp2 is not NULL"<<std::endl;
}
else
{
std::cout<<"pointer in sp2 is NULL"<<std::endl;
}
return 0;
}
输出:
(5)交换与赋值(Swap and assignment)
I、函数原型:void swap(shared_ptr& r); //交换*this 与 r 的内容
II、赋值操作符:operator= ,重载后可将shared_ptr 或 auto_ptr 对象赋值给 shared_ptr 对象。
原型如下:
[cpp] view plain copy
template<class T>
shared_ptr& operatork=(const shared_ptr<T>&r);
template<class T>
shared_ptr& operator=(const std::auto_ptr<T>& r);
};
例子6:
[cpp] view plain copy
#include <iostream>
#include <memory>
#include <string>
void isEmpty(std::shared_ptr<std::string>& r)
{
if(r)
{
std::cout<<"pointer in shared_ptr is not NULL"<<std::endl;
}
else
{
std::cout<<"pointer in shared_ptr is NULL"<<std::endl;
}
}
int main()
{
std::cout<<"before swap:"<<std::endl;
std::shared_ptr<std::string> sp1;
std::cout<<"sp1: ";
isEmpty(sp1);
std::shared_ptr<std::string> sp2(new std::string("hello world"));
std::cout<<"sp2: ";
isEmpty(sp2);
sp1.swap(sp2); //swap
std::cout<<"after swap:"<<std::endl;
std::cout<<"sp1: ";
isEmpty(sp1);
std::cout<<"sp2: ";
isEmpty(sp2);
std::cout<<"before operator=:"<<std::endl;
std::cout<<"sp1: "<<sp1<<", *sp1: "<<*sp1<<std::endl;
std::cout<<"sp2: ";
isEmpty(sp2);
sp2 = sp1; //assignment
std::cout<<"after operator=:"<<std::endl;
std::cout<<"sp1: "<<sp1<<", *sp1: "<<*sp1<<std::endl;
std::cout<<"sp2: "<<sp2<<", *sp2: "<<*sp2<<std::endl;
return 0;
}
输出:
(6)unique 与 use_count函数
函数原型:
[cpp] view plain copy
//use_count
long use_count() const; //返回所有指向共享资源的shared_ptr 指针对象的个数
//unique()
bool unique() const; //检查是否当前只有一个share_ptr 指针指向共享的资源。等价于:use_count() == 1
例子7:
[cpp] view plain copy
#include <iostream>
#include <memory>
#include <string>
int main()
{
std::shared_ptr<std::string> sp1(new std::string("hello world"));
std::cout<<"unique:"<<std::boolalpha<<sp1.unique()<<std::endl;
std::cout<<"count:"<<sp1.use_count()<<std::endl;
std::shared_ptr<std::string> sp2(sp1);
std::cout<<"unique:"<<std::boolalpha<<sp1.unique()<<std::endl;
std::cout<<"sp1 count:"<<sp1.use_count()<<std::endl;
std::cout<<"sp2 count:"<<sp2.use_count()<<std::endl;
return 0;
}
输出:
(7)reset函数
函数原型:
[cpp] view plain copy
//std::shared_ptr::reset
void reset();
template<class Y>
void reset(Y* ptr);
template<class Y, class Deleter>
void reset(Y* ptr, Deleter d);
该函数用 ptr 指针指向的资源替换掉当前shared_ptr 管理的资源,使 shared_ptr对象管理新的资源(pointered by ptr),以前资源对应的share_ptr 对象的引用计数减1。如果reset函数的参数为空,则表示*this(当前share_ptr 对象)退出共享资源。
例子8:
[cpp] view plain copy
#include <iostream>
#include <memory>
class A
{
private:
int m_x;
public:
explicit A(int x =0):m_x(x){}
int getX(){return m_x;}
int setX(int x){m_x = x;}
void print(){std::cout<<"A::print"<<std::endl;}
static A* alloc(int x)
{
A* pa = new A(x);
std::cout<<"a new object was created"<<std::endl;
}
static void free(A* pa)
{
std::cout<<"x: "<<pa->getX();
delete pa;
std::cout<<",A object was destroyed"<<std::endl;
}
};
int main()
{
std::shared_ptr<A> sp1(new A(10),&A::free);
std::shared_ptr<A> sp2(sp1);
std::shared_ptr<A> sp3(sp1);
std::cout<<"sp1 x :"<<sp1->getX()<<std::endl;
std::cout<<"sp2 x :"<<sp2->getX()<<std::endl;
std::cout<<"sp3 x :"<<sp3->getX()<<std::endl;
std::cout<<"sp1 count:"<<sp1.use_count()<<std::endl;
std::cout<<"sp2 count:"<<sp2.use_count()<<std::endl;
std::cout<<"sp3 count:"<<sp3.use_count()<<std::endl;
sp1.reset();
A* pa = new A(20);
sp2.reset(pa,&A::free);
std::cout<<"after reset:"<<std::endl;
if(NULL == sp1)
{
std::cout<<"pointer in sp1 is NULL"<<std::endl;
}
//std::cout<<"sp1 x :"<<sp1->getX()<<std::endl;
std::cout<<"sp2 x :"<<sp2->getX()<<std::endl;
std::cout<<"sp3 x :"<<sp3->getX()<<std::endl;
std::cout<<"sp1 count:"<<sp1.use_count()<<std::endl;
std::cout<<"sp2 count:"<<sp2.use_count()<<std::endl;
std::cout<<"sp3 count:"<<sp3.use_count()<<std::endl;
return 0;
}
输出:
(8)shared_ptr 在 STL 容器中的应用
由前面提到,shared_ptr 相比于 auto_ptr,可复制和赋值,故可用于 STL 容器中。
下面的例子9:将shared_ptr 放入容器中,并对每个容器中的元素进行操作,如使shared_ptr 指向的int 变量 变为原来的2倍。
[cpp] view plain copy
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
std::shared_ptr<int> double_it(const std::shared_ptr<int>& sp)
{
*sp *= 2;
return sp;
}
int main()
{
std::vector<std::shared_ptr<int>> numbers;
numbers.push_back(std::shared_ptr<int>(new int(1)));
numbers.push_back(std::shared_ptr<int>(new int(2)));
numbers.push_back(std::shared_ptr<int>(new int(3)));
std::cout<<"initially"<<std::endl;
for(std::vector<std::shared_ptr<int>>::const_iterator it = numbers.begin(); it != numbers.end(); it++)
{
std::cout<<*(*it)<<"(count = "<<(*it).use_count()<<")"<<std::endl;
}
std::transform(numbers.begin(), numbers.end(), numbers.begin(), double_it);
std::cout<<"after transformation"<<std::endl;
for(std::vector<std::shared_ptr<int>>::const_iterator it = numbers.begin(); it != numbers.end(); it++)
{
std::cout<<*(*it)<<"(count = "<<(*it).use_count()<<")"<<std::endl;
}
return 0;
}
输出:
(9)shared_ptr 在类层次结构中的应用
如 D 是 B 的子类,则可用shared_ptr类型的对象(基类指针)接收shared_ptr类型的对象(派生类指针)。
例子10:
[cpp] view plain copy
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class Item
{
private:
std::string title_;
public:
explicit Item(const std::string& title):title_(title){}
virtual ~Item(){}
virtual std::string Description() const = 0;
std::string getTitle()const {return title_;}
};
class Book: public Item
{
private:
int pages_;
public:
Book(const std::string& title, int pages):Item(title),pages_(pages){}
virtual std::string Description()const {return "Book: " + getTitle();}
int getPages()const {return pages_;}
};
class DVD: public Item
{
private:
int tracks_;
public:
DVD(const std::string& title, int tracks):Item(title),tracks_(tracks){}
virtual std::string Description() const {return "DVD: " + getTitle();}
int getTracks()const {return tracks_;}
};
int main()
{
std::vector<std::shared_ptr<Item>> items;
items.push_back(std::shared_ptr<Book>(new Book("C++ Primer",745)));
items.push_back(std::shared_ptr<DVD>(new DVD("MrVanGogh",9)));
for(std::vector<std::shared_ptr<Item>>::const_iterator it = items.begin(); it != items.end(); it++)
{
std::cout<<(*it)->Description()<<std::endl;
}
return 0;
}
输出:
(10)cast 操作符
C++ 中提供了四种强制类型转换操作符:static_cast, dynamic_cast, const_cast, reinterpret_cast。而关于shared_ptr 无法利用这些原始的操作符进行转换,其定义了自己的类型转换操作符:static_pointer_cast, dynamic_pointer_cast, const_pointer_cast 。
如【9】中提到的 “若 D 是 B的子类 ”,其为向上转换,但能否向下转换呢?即从 shared_ptr 到 shared_ptr (当然,前提是:shared_ptr 已指向了一个D对象,现在要做的就是“还原它”)。
I、使用 std::dynamic_pointer_cast,可以达到目的:
[cpp] view plain copy
template<class D, class B>
shared_ptr<D> dynamic_pointer_cast(const shared_ptr<B>& r);
该函数不会抛出任何异常(noexcept)。若执行成功(前提:shared_ptr对象 r 已经指向了一个D对象),则返回 shared_ptr 共享资源的所有权,否则返回一个空对象。
例子11:
[cpp] view plain copy
#include <iostream>
#include <memory>
#include <string>
class Item
{
private:
std::string title_;
public:
explicit Item(const std::string& title):title_(title){}
virtual ~Item(){}
virtual std::string Description() const = 0;
std::string getTitle()const {return title_;}
};
class Book: public Item
{
private:
int pages_;
public:
Book(const std::string& title, int pages):Item(title),pages_(pages){}
virtual std::string Description()const {return "Book: " + getTitle();}
int getPages()const {return pages_;}
};
class DVD: public Item
{
private:
int tracks_;
public:
DVD(const std::string& title, int tracks):Item(title),tracks_(tracks){}
virtual std::string Description() const {return "DVD: " + getTitle();}
int getTracks()const {return tracks_;}
};
int main()
{
std::shared_ptr<Item> spi(new DVD("MrVanGogh", 9));
std::cout<<"spi counter: "<<spi.use_count()<<std::endl;
std::shared_ptr<Book> spb = std::dynamic_pointer_cast<Book>(spi);
if(spb)
{
std::cout<<spb->getTitle()<<", "<<spb->getPages()<<std::endl;
}
else
{
std::cout<<"pointer in spb is NULL"<<std::endl;
}
std::shared_ptr<DVD> spd = std::dynamic_pointer_cast<DVD>(spi);
if(spd)
{
std::cout<<spd->getTitle()<<", "<<spd->getTracks()<<std::endl;
}
else
{
std::cout<<"pointer in spd is NULL"<<std::endl;
}
std::cout<<"spi counter: "<<spi.use_count()<<std::endl;
std::cout<<"spb counter: "<<spb.use_count()<<std::endl;
std::cout<<"spd counter: "<<spd.use_count()<<std::endl;
return 0;
}
输出:
II、static_pointer_cast
根据 static_cast 的知识:编译器隐式执行的任何类型转换都可以由static_cast 显示完成(如 int -> char;); 如果编译器不提供自动转换,使用 static_cast 来执行类型转换也是很有用的(如,找回存放在 void* 指针中的值)。
注意:static_cast 转换的一个特点就是:它只会生成原变量的副本,不会对原变量有任何修改。
而static_pointer_cast 工作的前提是:static_cast