c_cpp 智能指针
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 智能指针相关的知识,希望对你有一定的参考价值。
#include <iostream>
using namespace std;
// =============== smart pointer for int ==============================
class smtptr {
int *ref;
int *count;
public:
smtptr(int *p) {
if(!p) return;
ref = p;
count = new int; // gist
*count = 1;
}
smtptr(smtptr &p) {
ref = p.ref;
count = p.count;
(*count)++; // gist
}
smpptr& operator = (smtptr &p) {
if( *count > 0) remove_one_ref();
if(this == &p) return *this; // gist
ref = p.ref;
count = p.count;
(*count)++;
return *this; // gist!
}
int& operator * () { return *ref; }
int* operator -> () { return ref; }
protected:
void remove_one_ref() {
(*count)--;
if(*count == 0) {
delete ref;
ref = NULL; // gist
delete count;
count = NULL; // gist
}
}
};
// ===================== generic smart pointer ========================
template<class T>
class sun_ptr {
T *ref;
int *count;
public:
sun_ptr(T *ptr) {
ref = ptr;
count = new int;
*count = 1;
}
sun_ptr(sun_ptr<T> &ptr) {
ref = ptr.ref;
count = ptr.count;
(*count)++;
}
~sun_ptr() { remove_one_ref(); }
sun_ptr<T>& operator=(sun_ptr<T> &ptr) {
if( *count > 0)
remove_one_ref();
if(this == &ptr) return *this;
else {
ref = ptr.ref;
count = ptr.count;
(*count)++;
return *this;
}
}
T& operator*() { return *ref; }
T* operator->() { return ref; }
T getCount() {
return *count;
}
protected:
void remove_one_ref() {
(*count)--;
if(*count == 0) {
delete ref;
ref = NULL;
delete count;
count = NULL;
}
}
};
int main()
{
int *p = new int(99);
sun_ptr<int> ptr(p);
sun_ptr<int> ptr2 = ptr;
sun_ptr<int> ptr3 = ptr;
cout << ptr.getCount() << endl;
cout << *ptr;
return 0;
}
以上是关于c_cpp 智能指针的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp CPP - 教程015 - 智能指针和多态模板
c_cpp 使用unique_ptr,智能指针
c_cpp 指针迭代与C中的int指针
c_cpp 指针(*),地址在(@)
c_cpp C ++指针
c_cpp const指针