Proactive C ++ Reading Notes part5.implementations

Source: Internet
Author: User

26. Postpone variable definitions as long as possible.

Summary:

Delay variable definitions as much as possible. In this way, the program definition can be increased and the program efficiency can be improved.

Annotation:

The pure C language conflicts here, and the C language requires that the variable definition appear at the beginning of the Code. 27. Minimize casting.

1) const_cast is usually used to convert the constant property of an object to the constness (cast away the constness ). It is also the only C ++-style transformation operator with this capability.

2) dynamic_cast is mainly used to execute "Safe downcasting", that is, to determine whether an object belongs to a type in the inheritance system. It is the only action that cannot be executed by the old syntax and the only transformation action that may consume significant operation costs. 3) reinterpret_cast intends to perform a low-level transformation. The actual action (and result) may depend on the compiler, which means it cannot be transplanted. For example, convert a pointer to int into an int. This type of transformation is rare outside of low-level code. This book is only used once. When discussing how to write a debugging Allocator for debugging on the original memory (rawmemory), refer to article 50. 4) static_cast is used to force implicit conversions (implicit conversions). For example, convert a non-const object to a const object (as in Clause 3) or convert an int to a double object. It can also be used to perform reverse conversions of the preceding conversions, such as converting the void * pointer to the typed pointer and converting the pointer-to-base to the pointer-to-derived. However, it does not need to convert the const to non-canst'. Only the const_cast can do this. Summary:

First, if possible, try to avoid transformation, especially in efficiency-focused code to avoid dynamic_casts. If there is a design that requires transformation, try to develop an alternative design that does not require transformation.

Second, if transformation is necessary, try to hide it behind a function. The customer can then call the function without putting the transformation into their own code.

Third, we would rather use the C ++-style (New Style) transformation than the old style transformation. The former is easy to recognize, and has a strong role in classification. 28. Avoid returning "handles" to object internals.

Conclusion: avoid returning handles (including references, pointers, and iterators) to the object. Compliance with this clause can increase encapsulation, help const member functions behave like a const, and minimize the possibility of "dangling handles. Annotation: the virtual hanging number plate should indicate that the object to which it points has been destroyed, but the pointer or reference returned is still in use. 29. Strive for exception-safe code. When an exception is thrown. Functions with exceptional security: 1) do not disclose any resources. 2) data cannot be destroyed. Exception-safe functions provides one of the following three guarantees: 1) basic commitment: if an exception is thrown, everything in the program remains in the valid state. 2) strong guarantee: if an exception is thrown, the program State will not change. 3) packet throwing (nothrow) guarantee: promises not to throw exceptions, because they can always complete the functions they previously promised. Copy and swap principle: Make a copy of the object (original) You intend to modify, and then make all necessary modifications on that copy. If any modification action throws an exception, the original object remains unchanged. After all the changes are successful, replace the modified copy with the original object in an operation that does not throw an exception (SWAp ). Conclusion: first, exception-safe functions do not leak resources or allow corruption of any data structure even if an exception occurs. Such functions are classified into three possible guarantees: basic type, strong type, and no exception type. Second, "strong assurance" is often implemented using copy-and-swap. A single "strong guarantee" is not practical for all functions. Third, the "exception security guarantee" provided by the function is usually the weakest among the "exception security guarantee" of each function called by the function. Annotation: In the use of real programs, it is generally required to reach a strong type. Because the basic type may cause some data to be left out of use. The exception type is not thrown because the implementation is too complicated and generally does not reach this level. 30. The general concept of the understand the ins and outs of inlining. inline function is to replace "every call to this function" with the function ontology. Inline functions are usually placed in header files. Most compilers will complete inlining during the compilation period, some compilers will complete inlining during the link period, and even some compilers will complete inlining during the runtime. templates is usually placed in the header file, because once it is used, the compiler needs to know what it looks like to embody it. Some compilers will execute template ing only during the linking period, but in most cases, it is still implemented by the compiler. Whether a seemingly inline function is actually inline depends on your build environment and the compiler. Most compilers provide a diagnostic level: if they cannot convert the functions you require into line, they will give you a warning. Conclusion: first, limit most inlining to small and frequently called functions. This makes the debugging process and binary upgradability easier in the future, and minimizes potential code expansion problems, maximizing the program speed. Second, do not declare function templates as inline only because they appear in the header file. Annotation: templates and inline are irrelevant and need to be differentiated. 31. minimize compilation dependencies between files. compile dependency compilication dependency dependencies compile dependencies cascading compilation dependencies Summary: first, the general idea of supporting "minimal compilation dependency" is: dependent on declarative statements, not dependent on the definition. The two methods based on this idea are handle classes and interface classes. Second, the library header file should exist in the "full and declaration-only forms" situation. This method applies regardless of whether templates is involved. Sentiment: This book will be of great help to people who have a certain amount of code. If the amount of code is not enough, it may seem hard to understand and remember so many terms. Of course, even if the amount of code is enough, this book can not be read once, but can be read repeatedly in the case until the contents of these terms are integrated into the bone marrow. This part of content, my biggest feeling is that many things also bring negative side when it brings a favorable side, so we should carefully repeat it during the use process, how to use it. If there is no such usage, it will be foolproof, and it will certainly be better, depending on the specific use environment.

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.