Valid C ++ notes

Source: Internet
Author: User

Record the terms seriously agreed in the license tivec ++ simplified version

Clause 1: Try to use const and inline instead of # define

This clause is best referred to as "try to use the compiler instead of preprocessing", because # define is often considered not part of the language itself. This is one of the problems.

When you plan to use a template to write common functions such as max, first check the standard library (see article 49) to see if they already exist. For example, the above mentioned max, you will be pleasantly surprised to find that you can be cool for future generations: max is part of the C ++ standard library.
With const and inline, you have reduced the need for preprocessing, but you cannot completely ignore it. It is still a long time to discard # include. # ifdef/# ifndef also plays an important role in controlling compilation. Pre-processing cannot retire, but you must plan to give it a long vacation.

 

Clause 12: Use initialization whenever possible instead of assigning values in the constructor

 

Note that static class members will never be initialized in the class constructor. Static members are initialized only once during the program running. Therefore, it makes no sense to initialize the class objects whenever they are created. At least this will affect the efficiency: Since it is "initialized", why do we need to perform multiple times? In addition, the initialization of static class members is very different from that of non-static class members. This is specifically described in the m47 clause.

 

Clause 22: Try to use "Pass reference" instead of "pass value"

 

In c language, everything is implemented by passing values. c ++ inherits this tradition and uses it as the default method. Unless explicitly specified, the function parameters are always initialized through "Copy of real parameters". The caller of the function also obtains a copy of the function return value.

As I pointed out in the introduction of this book, the specific meaning of "passing an object by value" is defined by the copy constructor of the class of this object. This makes transferring values a very expensive operation.

 

Transferring references is a good practice, but it will lead to its own complexity. The biggest problem is the alias problem, which is discussed in Clause 17. In addition, more importantly, objects cannot be transferred by reference. For more information, see section 23. In the end, references are almost implemented through pointers, so passing objects through references is actually passing pointers. Therefore, if a small object, such as int, is used to pass a value, it is more efficient than transferring a reference.

 

 

Clause 28: global namespace Division

 

The biggest problem with global space is that it only has one. In large software projects, many people usually put their names in this single space, which inevitably leads to name conflicts.

Because of these restrictions, the real namespace should be used as early as possible once the compiler supports it.

 

 

Cla32: delay variable definition as much as possible

 

Yes, we agree that the variables in the C language should be defined in the module header; but in C ++, we should cancel this practice. It is unnecessary, unnatural, and expensive.

Remember? If you define a variable of the type of constructor and destructor, when the program runs to the definition of the variable, it will inevitably face the constructor overhead; when the variable leaves its life space, it must bear the expenses of the analysis structure. This means that defining useless variables is accompanied by unnecessary overhead, so as long as possible, this situation should be avoided.

As I know, your programming method is elegant and sophisticated. So you may be thinking that you will never define a useless variable, so the suggestions in this article do not apply to your rigorous and compact programming style.

 

Delaying variable definition can improve program efficiency, enhance program orderliness, and reduce comments on variable meanings. It seems that it is time to kiss the variable definitions of those open modules.

 

 

Cla34: Minimize compilation dependencies between files

 

 

 

 

Lostmouse Introduction

I have been familiar with CSDN for a long time, but it was not long ago that I started writing technical articles. It was even more accidental to open this column. For work reasons, I cannot guarantee that I will always have a large amount of time to maintain this column, but I will try my best.

As for the Lostmouse itself, it is not the clever and cute mouse in the cartoon; on the contrary, it is an old cat who is standing up but still confused: although the eyes are bright and firm, in my heart, I still don't know where the next goal is. Is the nature of a cat just waiting?

As a programmer, I like the saying "program life": How is it like writing a program! However, Lostmouse abused too many if, switch, break, and even for (;) in the past (;;). I hope that soon, the program I wrote will be more perfect and efficient.

In the photo, I fought with my 2-year-old son. The other character that didn't appear was my wife, Niu, I think everything I do has a meaning.

You are welcome to contact me with every friend on the way to program life!

Nettedfish@21cn.com
Nettedfish@yahoo.com

 

 

 

 

 

This article is from the "key code window" blog and will not be reprinted!

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.