template<typename T>
class RefCnt {
public:
RefCnt(T* ptr=nullptr)
:mptr(ptr)
,mcount(1)
{}
void addRef() { mcount++; }
int delRef() { return --mcount; }
private:
T* mptr;
int mcount;
};
template<typename T>
class CSmartPtr {
public:
CSmartPtr(T* ptr=nullptr)
:mptr(ptr)
,pmRefCnt(new RefCnt<T>(ptr))
{}
CSmartPtr(const CSmartPtr<T>& src) {
mptr = src.mptr;
pmRefCnt = src.pmRefCnt;
if (mptr != nullptr) {
pmRefCnt->addRef();
}
}
CSmartPtr<T>& operator=(const CSmartPtr<T>& src) {
if (this == &src) {
return *this;
}
mptr = src.mptr;
pmRefCnt = src.pmRefCnt;
if (mptr != nullptr) {
pmRefCnt->addRef();
}
return *this;
}
T& operator*()const {
return *mptr;
}
T* operator->()const {
return mptr;
}
~CSmartPtr() {
if (0 == pmRefCnt->delRef())
{
delete mptr;
mptr = nullptr;
}
}
private:
T* mptr;
RefCnt<T>* pmRefCnt;
};
int main() {
CSmartPtr<int> p1(new int());
CSmartPtr<int> p2 = p1;
CSmartPtr<int> p3;
p3 = p2;
cout << *p3 << endl;
*p3 = 20;
cout << *p3 << endl;
return 0;
}
class A {
public:
A(int data) {
cout << "A()" << endl;
}
~A() {
cout << "~A()" << endl;
}
void funcA() { cout<<"funcA()" << endl; }
private:
int ma;
};
class B {
public:
B(int data=0)
:_a(data)
{
cout << "B()" << endl;
}
void funcB() { cout << "funcB()" << endl; }
private:
A _a;
};
int main() {
B b;
b.funcB();
return 0;
}
class B;
class A {
public:
A() { cout << "A()" << endl; }
~A() { cout << "~A()" << endl; }
shared_ptr<B> _ptrb;
};
class B {
public:
B() { cout << "B()" << endl; }
~B() { cout << "~B()" << endl; }
shared_ptr<A> _ptra;
};
int main() {
shared_ptr<A> pa( new A());
shared_ptr<B> pb( new B());
//资源泄露
pa->_ptrb = pb;
pb->_ptra = pa;
cout << pa.use_count() << endl;
cout << pb.use_count() << endl;
return 0;
}
class B;
class A {
public:
A() { cout << "A()" << endl; }
~A() { cout << "~A()" << endl; }
void testA() { cout << "好方法" << endl; }
weak_ptr<B> _ptrb;
};
class B {
public:
B() { cout << "B()" << endl; }
~B() { cout << "~B()" << endl; }
void func() {
shared_ptr<A> ps = _ptra.lock();//提升方法
if (ps != nullptr) {//提升成功
ps->testA();
}
}
weak_ptr<A> _ptra;
};
int main() {
shared_ptr<B> pb(new B());
shared_ptr<A> pa(new A);
pa->_ptrb = pb;
pb->_ptra = pa;
pb->func();
cout << pa.use_count() << endl;
cout << pb.use_count() << endl;
return 0;
}
#include <thread>
class A {
public:
A() { cout << "A()" << endl; }
~A() { cout << "~A()" << endl; }
void testA() { cout << "好方法!" << endl; }
};
void handler(weak_ptr<A> pw) {
std::this_thread::sleep_for(std::chrono::seconds(2));
shared_ptr<A> ps = pw.lock();
if (ps != nullptr) {
ps->testA();
}
else {
cout << "对象已析构,非法访问!" << endl;
}
}
int main() {
{
shared_ptr<A> p (new A());
thread t1(handler, weak_ptr<A> (p));
std::this_thread::sleep_for(std::chrono::seconds(4));
t1.detach();
}
return 0;
}
#include <memory>
#include <thread>
#include <functional>
template<typename T>
class MyDeletor {
public:
void operator()(T* ptr) {
cout << "call MyDeletor::void operator()(T* ptr)" << endl;
delete[]ptr;
}
};
template<typename T>
class MyFileDeletor {
public:
void operator()(T* ptr) {
cout << "call MyFileDeletor::void operator()(T* ptr) " << endl;
fclose(ptr);
}
};
int main() {
unique_ptr<int, MyDeletor<int>> ptr1(new int[100]);
unique_ptr<FILE, MyFileDeletor<FILE>>ptr2(fopen("hello.txt", "w"));
unique_ptr<int, function<void(int*)>> ptr3(new int[100],
[](int* ptr)->void {
cout << "lambda function<void(int*)" << endl;
delete[]ptr;
}
);
unique_ptr<FILE, function<void(FILE*)>>ptr4(fopen("hello.txt", "w"),
[](FILE* p)->void {
fclose(p);
cout << "call function<void(FILE*)" << endl;
}
);
return 0;
}