智能指针(各有缺陷):
auto_ptr: 独占所有权---管理权转移
scoped_ptr: 独占所有权---防拷贝(简单粗暴)
shared_ptr: 共享所有权---增加引用计数
weak_ptr: 弱引用指针---shared_ptr的附属,
解决share_ptr循环引用带来的缺陷
C++封装的带智能指针链表:
#include <memory>
using std::tr1::shared_ptr;
struct linkNode
{
int data;
shared_ptr<linkNode> next;
};
class linkedList
{
private:
int length;
shared_ptr<linkNode> head;
public:
linkedList();
linkedList(const linkedList& theList);
bool empty()const
{
return length == 0;
}
int size()const
{
return length;
}
int get(int theIndex)const;
int indexOf(const int& theList);
void insert(int theIndex, const int& theElement);
void erase(int theIndex);
//void oouput(ostream& out) const;
};