最近在做Windows上管理USB手机终端的相关程序。
实际情况:
Class Phone *symbian = new Phone();
Class ConnectManager, Class CommManager都需要对symbian指针进行引用和维护(保留一个拷贝)。
但是这样就导致了symbian何时可以被delete?
在ConnectManager析构函数中delete? 这将导致CommManager无法使用symbian了。
在CommManager中delete symbian也是同样的道理。
在参考了《C++ Primer》之后,自己动手写一个智能指针
代码如下
运行结果如下:
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要由智能指针类去维护。