Predictional C ++

Source: Internet
Author: User

(Translation is too bad)

I. Syntax details
  1. C ++ does not allow you to modify temporary variables of built-in types.

  2. The default parameters are obtained based on the static type.

  3. The overloaded new and delete operators are always static, no matter whether you declare static or not.

Ii. Usage
  1. Select pre-increment. The post-increment is used only when you plan to use the initial value, and the post-increment is used to achieve post-increment.

  2. Note that temporary objects created by implicit conversions are hidden. A good way to avoid this problem is to use the explicit constructor to avoid writing conversion operators.

  3. Never # include unnecessary header files; never # include header files when the pre-declaration is sufficient

  4. Wise to use namespaces: if you put a class into a namespace, you must also put all auxiliary functions and operators in the same namespace.

  5. Never process arrays with polymorphism. the pointer type passed to the Delete [] operator must be consistent with its dynamic type.

  6. Const auto_ptr <> is a perfect practice for specific problems.

Iii. Principles
  1. Glsp: Generic liskov replacement principle: any type (or template) passed as a template parameter should comply with the requirements of this parameter

  2. Pay special attention to the exceptional security of the value assignment operator because it is different from the copy constructor.

  3. Give priority to cohesion and try to make each piece of code (every module, every class, and every function) have a single and well-defined responsibility; if a piece of code cannot be written into exceptional security due to internal design problems, it is almost always poorly designed. For example, it is difficult to write functions with two different responsibilities into exceptional security.

  4. Comply with the standard exception security rules: (1) Never allow exceptions to escape the destructor, reload the delete () and delete [] () operators; (2 ), use raiI to isolate resource ownership and resource management. (3) In each function, place all code that may throw exceptions and code that can safely handle such jobs to one side. Only when you are sure that the actual work has been completed, to modify the program status.

  5. Under what circumstances should public inheritance be used? We need public inheritance to represent the is-a relationship. All the rewritten member functions must be less demanding and do not promise less; never carry out public inheritance to reuse the code in the base class;

  6. Under what circumstances should I use non-public inheritance instead of inclusion? (1), we need to rewrite a virtual function; (2), we need to access protected members; (3), we need to construct the class to be used before another base class sub-object, you can also analyze the classes used after them. (4), we need to share the shared virtual base class, or rewrite the structure of the virtual base class; (5 ), we have benefited a lot from the optimization of the null base class; (6) We need "controlled polymorphism", but only in some code

  7. Interface rules: For a class X, all functions that "Mention" X and provide together with "X" (including free functions) are logically part of X, because they constitute a part of the X interface, the member functions and non-member functions may be logically part of the class. However, the member functions still have a stronger relationship than the non-member functions and classes; one useful way of interpreting "provided together" is to "appear in the same header file and/or namespace". If it appears in the same header file, they are part of the class in terms of dependency. If they appear in the same namespace, they are part of the class in the sense of Name Lookup.

Iv. Practice
  1. Use the technology you are most familiar with to construct the core part of the product, and test and learn new technologies that you are interested in the extended part.

  2. Remember the computer science motto: most problems can be solved by adding an indirect layer. Of course, it is wise to use Occam's razor to reconcile it: do not make things more complex than necessary

V. Viewpoint
  1. The most important contribution of C ++ standardization to C ++ is: more powerful support for "Powerful abstract concepts" to reduce the complexity of software;

  2. The most important advantage of object-oriented programming is to reduce software complexity by managing dependencies between codes.

Vi. Problems
  1. Does the standard library provide a fixed-length Array Packaging class? Is Vector enough?

  2. There is no auto_array_ptr <>, or is auto_ptr always used to replace the array for dynamic memory allocation?

Related Article

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.