上一页 1 ··· 9 10 11 12 13 14 15 16 17 ··· 31 下一页
摘要: 1. 成员函数的接口总是被继承. 如 Item32 所说, public 意味着 is-a, 所以对 base class 为真的任何事情对 derived class 也为真2. 声明一个 pure virtual 函数的目的是为了让 derived class 只继承其接口 但令人意外的是, 我们竟然可以为 pure virtual 函数提供定义. 这有另一种用处, 即作为 default 实现3. 声明 impure virtual 函数的目的是为了让 derived class 继承接口和默认实现这里会出现一个特殊情况, 如果有些子类(多数子类)欢迎默认实现而少数子类却不欢迎呢? 一. 阅读全文
posted @ 2014-03-01 13:01 SangS 阅读(156) 评论(0) 推荐(0)
摘要: class Base {private: int x;public: virtual void mf1() = 0; virtual void mf2(); void mf3(); ...};class Derived: public Base {public: virtual void mf1(); void mf4(); ...};上面代码构成了一个 namespace在 derived class 内的 mf4 代码实现为void Derived::mf4() { ... mf2(); ...}编译器看到 mf2 后现在 ... 阅读全文
posted @ 2014-03-01 12:44 SangS 阅读(151) 评论(0) 推荐(0)
摘要: 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)
上一页 1 ··· 9 10 11 12 13 14 15 16 17 ··· 31 下一页