摘要:
Too high class topic for me now.........................................................fill the blank to let evernote notice 阅读全文
posted @ 2014-02-28 16:22
SangS
阅读(151)
评论(0)
推荐(0)
摘要:
1. A good API will provide easy to use interfaces but also provide hard to miss-use interfaces. Usually the later one is more fundamental than that of the previous one. Consider you want to write a data class, there are thousands ways to write it. Here is one example:class Date {public: Date(int ... 阅读全文
posted @ 2014-02-28 16:07
SangS
阅读(204)
评论(0)
推荐(0)
摘要:
If you trying to do multiple things in one statement, you should think carefully abnormal behavior especially exceptions. Let's see an example:processWidget(std::tr1::shared_ptr(new Widget()), priority());What you expected might benew Widget;shared_ptr(...);priority();But it may execute in the o 阅读全文
posted @ 2014-02-28 15:37
SangS
阅读(176)
评论(0)
推荐(0)
摘要:
1. When you created an array and want to return the memory to system. You need to explicitly add [] to show that that's an array you want to return, so more than one destructors will be called.2. However, things might not be that simple. Imagine you are fixing a bug and trying to delete an objec 阅读全文
posted @ 2014-02-28 14:48
SangS
阅读(165)
评论(0)
推荐(0)
摘要:
In last two item, I talk about resource-managing using RAII, now comes to the practical part. Often, we encounter a situation where an API accept raw resource instead of RAII object. For example:// You have a shared pointerstd::tr1::shared_ptr foo(createFoo());// But the API only accept Foovoid acce 阅读全文
posted @ 2014-02-28 14:33
SangS
阅读(282)
评论(0)
推荐(0)
摘要:
In C++, the only code that guaranteed to be executed after an exception is thrown are the destructors of objects residing in stack and that's why we need RAII. We don't always deal with head based objects, so instead with using auto_ptrs and shared_ptrs, sometimes we need to create our own r 阅读全文
posted @ 2014-02-28 12:37
SangS
阅读(316)
评论(0)
推荐(0)
摘要:
1. Always use object to manage resource! If you delete a pointer or release a handler manually by yourself, there is great chance that you will make mistake or forget something.2. There are two critical aspects of using object to manage resources: 2.1 Resources are acquired and immediately turned o. 阅读全文
posted @ 2014-02-28 11:21
SangS
阅读(182)
评论(0)
推荐(0)
摘要:
This one is simple, do not forget to copy all parts of an object in copy constructor or assignment operator!There are two cases you tend to make mistakes.1. Make sure you modify copy constructor and assignment operator, if you add a member to a class.2. Make sure you call the copy constructor and as 阅读全文
posted @ 2014-02-28 10:45
SangS
阅读(305)
评论(0)
推荐(0)
摘要:
If you want to concatenate assignment like thisint x, y, z;x = y = z = 15;The convention is to make the assignment operators return a reference to *this.11 Handle assignmnet to self in operator =You can easily assign an object to itself without knowing it// Example 1Foo *px, *py;px = py;//Example 2a 阅读全文
posted @ 2014-02-28 00:00
SangS
阅读(276)
评论(0)
推荐(0)

浙公网安备 33010602011771号