"Effective C + +" study notes (i)

Source: Internet
Author: User

original articles, reproduced please specify the source: http://blog.csdn.net/sfh366958228/article/details/38701767
Objective

"C + + Primer" and "effectivec++" has been recognized by C + + programmers Red Book books, this time brought is "effective C + +" study notes and experience.

"Effective C + +" is a term book, so when you look at it is not exactly in accordance with the order of the book to see.

Well, no more nonsense, let's get to the point.

Article 01: Treat C + + as a language federal

The original understanding of C + + was added to the C language of the object concept on the surface, which was not found after learning C + + Primer.

Today's C + + is already a multi-paradigm programming language, a language that supports both process forms, object-oriented forms, function forms, generic forms, and meta-programming forms. So there seems to be an exception to all "proper usage", which, while using a variety of C + + high-performance programming codes, should vary and should not be used rigidly.


Summarize:

The code for efficient programming of C + + varies depending on what part of C + + you use.

Clause 20: Use "constant reference passing (Pass-by-reference-to-const)" instead of "value passing (pass-by-value)"

By default, C + + is passed as a value (inherited from the C language) to pass the object into the function. Unless otherwise specified, the parameters of the function are initialized with the copy of the actual parameter, which is the copy constructor parameter of the object, and finally the destructor is released, which in this process will cause unnecessary waste of resources.

Especially in a class of complex structure, for example:

Class Student:public Person {public:     Student ();     ~student ();p rivate:     std::string name;}

The student class inherits from the person class, and there is also a member variable of type string, so when he is copied, it is not only to be constructed and destroyed, but the name, the person class, and the member variable of the person class, the parent class of the person class, etc., must be constructed and refactored.

So you can optimize the function of the value passing method as follows

BOOL Validatestudent (Student s);//optimization before bool Validatestudent (const Student &s);//Optimized


In this way, efficiency will be greatly improved.
Furthermore, passing by reference avoids the case where a derived class is treated as a base class (because the derived class creates a copy of the base class property through the copy constructor of the base class).

However, it is not always possible to use reference delivery more efficiently than using a value, because reference passing often means passing pointers, so if the arguments are built-in types, passing values is more efficient than referencing.

In STL iterators and function objects, the use of value-passing efficiency is also higher than the reference pass, because they are traditionally designed to be passed as values.

Summarize:

1) As far as possible by reference to pass instead of value, the former is often more efficient, and can avoid cutting problems.

2) This rule does not apply to built-in types, as well as STL iterators and function objects. For them, value delivery is more efficient.

Clause 21: When you must return an object, don't be paranoid about returning it referenceWhen an object is a local object, when the function ends, the object is destroyed, and if it returns a reference, it will fall into the "undefined behavior" because it points to an object that has been destroyed.If within a function, the object is created in the heap by new, it still has to be constructed once, and at the same time, there is another problem: who should delete your new object? Even if the person using the function may delete, but in good sense, it is best not to use it.Neither the heap nor the objects created in the stack can return the Reference,static object, because it will result in the same if two of the functions that are different results are called at the same time.

Summarize:

Never return a pointer or reference to a local stack object, or return a reference to a Heap-allocated object, or return a pointer or reference to a local static object and possibly require multiple such objects at the same time.

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.