Highly effective effective C + + 55 Personal Learning Note two

Source: Internet
Author: User




Terms one: in operator = inside processing self-assignment

This clause ensures that when an object is self-assigned operator= has a good behavior and must be judged and compared to the object. Note the copy and swap technology ,

Also note that when a function operates on multiple objects, it can also be judged.

Terms : Don't forget every ingredient when copying objects

For the replication of objects we have to ensure their integrity, then the key place is the copy construction and = assignment operation, you must do every data

All members are copied to the new object. It is important to note that when inheriting subclass replication, each member of the parent class must be considered, in two ways, a

Remember to add the initialization of the parent class member list, one is to start from the parent class each member to replicate, progressive, this is a better way.

Terms : Managing Resources with Objects

Object resource management is a more straightforward method of resource management. Based on C + + on constructors, destructors, copying functions ,

As long as the training, follow these methods, the practice of research and development process will basically eliminate the problem of resource management. RAII(Resource acquisition is

initialization This is an effective way of managing resources to prevent leaks. The two commonly used classes are the tr1::shared_ptr class and the tr1:auto_ptr class .

The essence of RAII is to use objects to represent resources, to transform the task of managing resources into the task of managing objects, and to acquire and release resources from objects.

Constructs and destructors correspond to ensure that the resource is always valid for the lifetime of the object, and that the resource must be freed when the object is destroyed. Other words

Owning an object is equal to owning the resource, and the resource must exist if the object exists.

Terms copying : Be careful in the resource management category Behavior

This clause when you use Object management resources, sometimes you do not want objects to be copied, or allow replication. Then you have to decide your copying function .

whether the compiler is automatically generated or you define it yourself . such as suppressing copy or using the counting method is also a good choice. Remember to copy objects must

To make a deep copy, copy each of its members.

Terms : Provide access to the original resource in the resource management class

This clause is required to be taken into account when you need to access the original object through this resource management object.

For example, you want to invoke and manipulate some of the Class1 methods defined in class2 . So at this point you have to provide an external interface conversion in Class2 .

There are two things that must be considered. One is to display conversion functions such as

Class1 get () {return Class1}; the other is an implicit conversion function such as operator Class1 () const {return Class1};

It is generally shown here that conversions are more straightforward and secure, but implicit conversions are more straightforward when used.

Terms : Pairs use new and delete to take the same form

When this clause is used, tell us that we usually Call the operatornew and the delete by default The Operatordelete function .

( there are actually several forms of new and delete .) For example, new has three ways to use:plain New,nothrow new and placement New .

There are also three kinds of correspondence Delete We are the first to do this by default, just remember that new [] and delete[] will appear in pairs .

Terms : Placing the newed object into a smart pointer with a separate statement

This clause reminds the user when calling, especially when a function parameter is passed in. Such as

Processwidget (new widget,priority);

Processwidget (std::shared_ptr<widget> (new Widget), priority)// note here . In accordance with the resource management terms we learned earlier,

To prevent leaks, place the object inside a smart pointer. There is no problem with this idea, and then in the process of passing this parameter, there is a big problem.

The stacking order of different compiler parameters is not the same. If new succeeds, then the Execute priority function fails to return . Then our object pointers are

was completely lost. This results in a new memory leak. Remember that you must independently place objects in a smart pointer with separate statements. So sometimes,

Shorthand code is not necessarily a good thing. Because of the uncertainty of compiler compiler parameters caused by man-made.

Terms The interface is easy to use correctly and is not easy to misuse

C + + interfaces include , function interfaces, class interfaces, template interfaces, and so on. Good interfaces are more easily used, such as in type creation and parameter interface processing

, validation of its validity and legitimacy, we will be in the design of consistency, compatibility judgment. This can improve robustness.

Tr1::shared_ptr supports custom-made filters . Note that this can prevent DLL problems and can be used to automatically unlock mutexes.

Terms : Design class like a design type


Here you have to take the macro into account that your class is already a new type. So as a class that you design yourself. Good class design, you have to think very

Multi-case: How the new type object should be created and destroyed, the difference between initialization and assignment, the type 's data member legal value is constrained ,type

Need to be well-equipped with the inheritance diagram,type need to convert, you must design a new type class , and so on.

Terms : Ning to pass -by-reference-to-const Replace pass-by- va Lue

When it comes to this clause, it is stated that the pass-through parameter is in the form of a copy by default in C + +. Which will create a temporary one.

The copy. In most cases, it is more efficient to pass a constant reference directly, because it avoids the overhead of two of copies. But the

Rules are not adapted to all situations, such as built-in types and STL iterators and Function Objects , which are more appropriate for pass-by-value.

Highly effective effective C + + 55 Personal Learning Note two

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.