"Effective C + +" key Summary (i)

Source: Internet
Author: User

At the outset.

This week no longer published on the data structure of the blog, want to take half a month to see the book to do some summary, finishing, the first is "effective C + +". The first time to read this book is more than a year ago, preparing for the examination of postgraduate examinations, and then continued, this month again to see is the third time, have not seen the "deep Exploration of C + + object Model", so some places are not very thorough (now some places also can not see, but better than before). There is another book is the "Deep Exploration of C + + object Model", see the second time, absorb more easily, read always so, the first time may not know anything, but as long as the insistence on watching, back to read again, many things, naturally understand.
"Effective C + +" sequence of the blog, I will be the original book in the chapter, as far as possible to express in descriptive words the original book advocated practice and the reasons for doing so, there are a lot of considerations in the book, but I will not record those very thin, I will do is to refine what I think is more important or easy to remember those , why so? What is important is often about efficiency and safety, so it is enough to warn us. And easy to remember, it can be easier for us to accept, in practice to apply. In the process of description, in addition to refining the meaning of the book, I will join through the "Deep exploration of the C + + object Model" to get the understanding, will also add some about c++11 content, c++11 to c++98 a lot of improvements, and "effective C + +" is mainly for c++98, So there are a few provisions may no longer apply to c++11, but based on c++98 code a lot of, school teaching is also mainly c++98, so even in c++11 no longer applicable to the content, I will not remove, just provide a feasible practice in c++11. Finally declare: Because I am only a beginner of C + +, so there must be many incorrect places, also please point out, I humbly accept, but refused to have offensive language, I hope you can be a gentleman to communicate.

Overview of the effective C + +

Effective C + + is written by Scott Meyers, one of the world's most well-known C + + software development experts, that outlines the recommendations that should be followed when writing software in C + + in terms of articles that lead to the trend of writing technical books in this form. The book is divided into 9 chapters, a total of 55 articles, covering efficiency, security, programming style, object-oriented, language itself all aspects. The headings of each chapter are as follows:

    1. Make yourself accustomed to C + + (4 section)
    2. Construction/destructor/assignment operations (8 models)
    3. Resource Management (5 articles)
    4. Design and declaration (8 articles)
    5. Implementation (6 article)
    6. Inheritance and object-oriented design (9 articles)
    7. Templates and generics programming (8 models)
    8. Customizing the New and delete (4 articles)
    9. Miscellaneous discussion (article 3)

The book Translator Houtie is I have been very fond of the writer, with 700 pages of the big "easy to read MFC", a thorough and clear "STL Source analysis." Translated by "Deep Exploration C + + object Model", "effective C + +", "more effective C + +", "Memory limited system software development" and so on (I have only listed what I have read). The quality of these works is very high, many of them are classic, well worth reading.

"Effective C + +" chapter I: Make yourself accustomed to C + +

1.c++ is not a purely object-oriented language, nor is it a simple extension of C. It is a hybrid, mixed object-oriented, process-oriented, generic programming three forms, various forms can be mixed use, can also be used alone, so you should lock the programming principle should be based on the type of code to use.
2. Replace # define with const, enum, and inline as much as possible. One reason is that these keywords can clearly express their intentions. The second reason is more secure. The third reason is that the transplant procedure is more convenient. So constant use of const or enum, frequently used small functions please use inline, if the use is not very frequent, then you need to weigh the code expansion problem.
3. Use const as much as you can.

1)ConstYou can control the reading of the pointer. Based on its relative position with the * number, ifConstOn * Left (Const Type* andType Const* All is a meaning, is legal), then the pointer refers to the object can not be modified, if on the right side indicates that the pointer itself can not be modified, if both sides have, then neither can be modified. Précis-writers is the "Left thing right needle". (If you remember correctly, the usage of the balance is "left thing right code"?).2The STL iterator is modeled as a pointer, so the iterator acts like a t*, so declare aConst Iteratoris equivalent to declaring aConstpointers, which indicate that the pointer itself cannot be modified, use Const_iterator if the object that the iterator is pointing to cannot be changed.3) makes the function return aConstTo reduce the number of errors caused by a user error, which causes the compiler to discover these errors. The premise is to return thisConstThe value of the type is semantically correct.4) willConstActing on member functions can indicate which interfaces can alter the object's content, and which excuses are not. You can also make the actionConstObject "becomes possible.5The constant nature of the return value can be used as the basis for function overloading, which represents read-write control over the return value of a function. If a constant value is returned, the value cannot be written.6The compiler performs bitwise-constness semantics, but when writing programs, it may be more necessary to conceptual-constness semantics, beware.7) Let's return to non-ConstA member function of a type calls itsConstVersion of the Member brothers to reduce the duplication of code, and doing so is safe, not the reverse.

4. Initialize the variable.

1)  让对象在被使用前都先被初始化。2)  初值列表比赋值操作性能更好,所以尽量使用初值列表,但需要注意初值列表中变量在声明时的顺序,初始化顺序是与之相同的,而与初值列表中变量的顺序无关。base classes更早于其derived classes被初始化。3)  staticstatic对象。具体做法是:(1)创建一个类A。(2)编写一个函数f,在该函数里声明一个A类型的静态对象b,然后返回b的引用。

"Effective C + +" key Summary (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.