"Effective C + +" study notes-clause 26

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************



Zatan

"Effective C + +" has seen a 26th clause, a total of 55, looked at about half.

Just started reading this book, or because then wanted to improve C + +, and then search the book to see,

At that time did not know where to see a sentence:

The people who learn C + + are divided into two types who have read the book and have not read it. It was true that you had either read it or didn't read it, but I thought more ... )

The book is very high-rated, and then determined to read it.

Nearly half of the terms look over, the real receipt of goods is really not small, because all the things in the book, I have not all touched,

Therefore, have not understood also to search a search, turn over, sometimes a clause to watch for a few days.


Immediately after the winter vacation, tomorrow the last exam, about the algorithm, winter vacation put nearly 2 months,

Have a good plan ...




V. Implementations (Implementation)

Well, yes, it's the fifth chapter. ~

Each chapter has its own theme, such as

The first chapter of some basic things (to familiarize you with C + +),

The second chapter constructs a class of spine (good aggregation structure, destruction, assignment operations),

The third chapter of the resource management,

The fourth chapter of the design and declaration of the interface.

Chapter?

This chapter focuses on the solution of a series of things that need to be taken care of when presenting your own class (or class template) definition and functions (or function template).



Rule 26:postpone variable definitions as long as possible.

Clause 26: Delay the occurrence of variable definitions whenever possible.




1. Why?

As long as we define a variable and the type has a constructor or destructor, then when our program reaches the definition of the variable, we have to bear the cost of the construction, and when our variables leave the scope, we will have to take the cost of the destruction. Even if this variable is not used.

To sum up one sentence- too fast defining a variable can result in a delay in efficiency .



2. Questions

<1> What if I decide I can't define a variable that I don't use?

Look at this function:

std::string encryptpassword (const std::string& password) {    using namespace std;    string encrypted;    if (Password.length () < Minimumpasswordlength)  {    throw logic_error ("Password is too short");    }    ...    Necessary action, place an encrypted password into the variable encrypted    return encrypted;}


The object encrypted is not completely unused in this function, but if an exception is thrown, it is not actually used.

So, even if your variable is defined within the function and used, but if an exception is thrown, the cost of construction and destruction is still to be borne, but in fact this variable is useless .


Amount: Perhaps, put the definition after throwing an exception?

Like this:

std::string encryptpassword (const std::string& password) {    using namespace std;    if (Password.length () < Minimumpasswordlength)  {    throw logic_error ("Password is too short");    }    string encrypted;    ...    Necessary action, place an encrypted password into the variable encrypted    return encrypted;}


But this code is still not fit enough, because encrypted is defined without any arguments as the initial value. This means that the call is its default constructor.

In Clause 4, it explains why "it is inefficient to construct an object from the default constructor and then assign it" than to specify the initial value directly at construction .

So, what's really popular is this:

std::string encryptpassword (const std::string& password) {    ...    Std:string encrypted (password);        Encrypt (encrypted) is defined and initialized by the copy constructor;    return encrypted;}


Since then, the true meaning of "delaying as much as possible" has been known.

You should not only postpone the definition of a variable until the first moment of the variable is used, but you should even try delaying the definition until you can give it an initial argument.

Doing so not only avoids the need to construct (and refactor) non-essential objects, but also avoids meaningless default construction behavior. In a deeper layer, the variables are initialized with the "initial value of the obvious meaning" and the purpose of the variable can be attached.


<2> What about loops?

If a variable is used only within a loop, is it better to define it outside of the loop and assign it to it at each iteration of the loop, or should it be defined in the loop?

Method A is defined in the loop outside widget w;for (int i = 0; i < n; ++i) {    W = depends on a value of I;    ...} Method B: Defined in the loop for (int i = 0; i < n; ++i) {    Widget W (depending on a value of i);    ...}


Let's start by calculating the cost:

? Procedure A: A constructor + 1 destructors + N assignment operations

? Procedure B:n A constructor + N destructors


So if one of the assignment costs of class is less than a set of construction + analysis composition, practice A is generally more efficient. Especially when the value of n is very large.

Furthermore the procedure a causes the scope of the name W to be larger than the procedure B, which sometimes conflicts with the understandable nature and ease of maintenance of the program.

Therefore, unless:

① know that the cost of assignment is lower than construction + destruction.

② you're working on a highly efficient part of your code.

Otherwise, you should choose Method B.



3. Please remember

☆ Postpone the occurrence of variable definitions as much as possible. This can increase the clarity of the program and improve the efficiency of the program.




*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************

"Effective C + +" study notes-clause 26

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.