pie*_*fou 1 c++ observer-pattern
Observer *o = New Observer();
Subject *s = new Subject() ;
s->register(o);
//Is it a good practice to delete the observer in the unregister function?
//I feel it is not. As the Observer object might still be in use, for example ,
//it might be registered to another Subject.
s->unregister(o);
//So it is safe to rely on the client code to delete the object or rely on the smart pointer things
delete o;
Run Code Online (Sandbox Code Playgroud)
我想确认关于谁应该删除观察者对象,我的上述理解是否正确.
我同意你的观察.在取消注册函数中删除观察者并不是一个好习惯 - 因为"创建资源的人必须负责删除资源"这一简单事实
这样可以避免
在所有具有不同术语的书籍中讨论了类似的主题.
| 归档时间: |
|
| 查看次数: |
825 次 |
| 最近记录: |