re: 智能指针源码 fr3@K 2008-07-16 01:45
std::auto_ptr 就可以. 请参考这篇 (http://www.gotw.ca/publications/using_auto_ptr_effectively.htm) 的 example 7 的用法说明.

实作上可以参考这里 (http://code.google.com/p/gion/source/browse/branches/0.1/include/gion/auto_array.hpp), 找与 auto_array_ref 相关的代码.
re: 智能指针源码 fr3@K 2008-07-11 14:16
你的实现没办法像这样使用:

AutoPtr<int> foo()
{
return AutoPtr<int>(new int);
}

void bar()
{
AutoPtr<int> p = foo();
}
re: C++代码风格谷歌版 fr3@K 2008-07-10 13:42
小弟认为完全地禁用异常并不可取, 详见:
http://fsfoundry.org/codefreak/2008/07/06/google-forbids-use-of-exception-in-cpp/
在 CTest 里面用 boost::shared_ptr 保持 CTestImp 的指针会有问题. 这样会造成不同的 CTest 对象对应到同一个 CTestImp 对象.
CTest a;
CTest b(a);
也就是说 a.pimpl_.get() 会等於 b.pimpl_.get(). (假设 CTest::impl_ 为 public)

我想这不是你想要的结果.
@蚂蚁终结者

我的考量多是站在 library writer (职业病) 的角度。
Library 常需要在程式一 load 上来就把 static (global) instance 启始好. 总不好要求使用者进入 main() 才能使用.

希望你能把进入 main() 之后再呼叫 Instance() 这段加入你的文章里面, 以免误导了读者如我辈.

我是尽量避免 lazy initialization 的人, 尤其是在 static instance (singleton) 的创建上. Lazy initialization 的策略有可能造成譬如说一个 global logger 在某个对象的虚构函数内第一次被使用 (或许可能性很小, 但毕竟不能 100% 排除), 而我们又无法保证该 logger 肯定会被创建成功. 这样是否代表我们必须在每个使用到 logger 的 destructor 内部做 try-and-catch? 等等问题...
已知 : global (static) instance 在 multithreading 下有启始上的问题。
试问 : 如何用一个 global instance (mutex) 去确保另一个 global instance 的启始正确?

请参考拙著 Is your Singleton Broken? (http://fsfoundry.org/codefreak/2006/05/05/is-your-singleton-broken/)
<2026年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

公告

这个帐号的主要用途是在 CPPBLOG 留言交流. 个人博客位于 http://fsfoundry.org/codefreak/

常用链接

留言簿

搜索

最新评论