C++ 智能指针

C++ 智能指针

 

 

最近在做Windows上管理USB手机终端的相关程序。

 

实际情况:

Class Phone *symbian = new Phone();

Class ConnectManager, Class CommManager都需要对symbian指针进行引用和维护(保留一个拷贝)。

但是这样就导致了symbian何时可以被delete?

在ConnectManager析构函数中delete? 这将导致CommManager无法使用symbian了。

在CommManager中delete symbian也是同样的道理。

 

在参考了《C++ Primer》之后,自己动手写一个智能指针

 

代码如下

 

 

#include <iostream> using namespace std; class ConnectManager; class CommManager; class Phone { public: Phone() { cout << "Hello World!" << endl; } ~Phone() { cout << "Bye!" << endl; } }; class U_ptr { friend class ConnectManager; friend class CommManager; public: U_ptr(Phone *pH): use(0), pPhone(pH) { } ~U_ptr() { delete pPhone; } private: Phone *pPhone;//这里是关键,转移数据的宿主 size_t use; }; class ConnectManager { public: ConnectManager(U_ptr *p): pUptr(p) { ++pUptr->use; cout << "ConnectManager: Hi I get the Phone" << endl; cout << pUptr->use << " users" << endl; } ~ConnectManager() { cout << "ConnectManaer: Can I delete the Phone?" << endl; if (--pUptr->use == 0) { cout << "Yes, You can" << endl; delete pUptr; } else { cout << "No, You can't, The Phone is in use" << endl; cout << pUptr->use << " users" << endl; } } private: U_ptr *pUptr; }; class CommManager { public: CommManager(U_ptr *p): pUptr(p) { ++pUptr->use; cout << "CommManager: Hi I get the Phone" << endl; cout << pUptr->use << " users" << endl; } ~CommManager() { cout << "CommManager: Can I delete the Phone" << endl; if (--pUptr->use == 0) { cout << "Yes, You can" << endl; } else { cout << "No, You can't. The Phone is in use" << endl; cout << pUptr->use << " users" << endl; } } private: U_ptr *pUptr; }; int main(void) { Phone *symbian = new Phone(); U_ptr *pU = new U_ptr(symbian); ConnectManager connManager = ConnectManager(pU); CommManager commManager = CommManager(pU); } 

 

 

 

运行结果如下:

 

Hello World!

ConnectManager: Hi I get the Phone

1 users

CommManager: Hi I get the Phone

2 users

CommManager: Can I delete the Phone?

No, You Can't. The Phone is in use;

1 users

ConnectManager: Can I delete the Phone?

Yes. You can

Bye!

 

 

代码中已经注释出来了。智能指针的关键是转交数据的宿主。虽然Phone是给CommManager和ConnectManager使用,

但是,真正的Phone要由智能指针类去维护。

 

 

 

上一篇:Exams/ece241 2013 q12


下一篇:总结: Redis 查看key大小的简单总结