Effective C + + 11-17

Source: Internet
Author: User

11. Declare a copy constructor and an assignment operator for a class that needs to dynamically allocate memory.

Obviously, due to the dynamic memory allocation, there will definitely be a deep copy of the problem, to rewrite the copy constructor, so that it is a dark copy, in order to achieve a true meaning of the copy. This is what I understand about the reason that you want to declare a copy constructor.

And for the assignment operator, a similar truth.

A b = A;b = A;
For both of these forms, the copy constructor is called, and the following is the assignment operator =. Assignments are similar to replication, and the default operation is to replicate all members of the class.

Deep copy the main operation is very simple, for pointers, the dynamic application of a piece of memory to hold the pointer to the data, each pointer to its own piece of memory, and not others.


12. Try to use initialization instead of assigning values in constructors.

That is, use the member initialization list as much as possible, rather than using the assignment method.

First, for const members and references, you can initialize only by using the initialization list. The second initialization list is more efficient because the object is created in two steps: The initialization of the data member and the execution of the action in the called constructor body. The initialization of the data member, and then the assignment, is performed even before the assignment. So using an initialization list is more efficient.

However, when there are a large number of fixed-type data members to initialize in the same way in each constructor, it is more reasonable to use the assignment.


13. The members in the initialization list are listed in the same order as they are declared in the class.

This is because the order in which the lists are initialized does not affect the order in which they are initialized, and the order in which they are initialized is determined by the order in which they are declared in the class, and the order in which they appear is initialized in the order in which the program appears to initialize the list.

C + + does not use the order of the initialization list because the object's destructor is created in the reverse order that the member was created in the constructor. If the object is not initialized in a fixed order, the compiler will record the initialization order of each object member, which is a significant overhead.


14. Determine that the base class has a virtual destructor.

When you remove an object from a derived class by using a pointer to a base class, the base class must have a virtual destructor, otherwise there will be unpredictable consequences. No virtual destructor is used, only the destructor of the base class is called to remove the derived class object, which is not possible, and cannot be determined.

Constructor calls are derived classes after the base class, and the order of the destructors is derived from the base class after the first class.

Using a virtual destructor is a bad idea when a class is not used as a base class. Because an object of a virtual function has a virtual pointer pointing to a virtual table, it wastes space to store the meaningless pointer.

The pure virtual function is added after the virtual function = 0.


15. Let operator = return a reference to *this.

The = sign can be connected because of the reason for its return value, the form of the declaration operator= as follows:

c& C:: operator= (const c&);
Both the input and the return are references to the class object. To achieve continuous assignment operations. The return value is = The reference to the left value, which is the *this reference, because the parameter on the right is the const type.
The argument to the function is the const type because the value on the right is calculated to get a new result, and the object is stored with a temporary object that is a const type because it is a parameter of a function and cannot be modified by a function.


16. Assign values to all data members in operator =.

For the deep copy to write itself a more correct = operation.

When inheritance is involved, the assignment operation of the derived class must handle the assignment of the base class. If you override an assignment operation for a derived class, you must assign a value to the base class part that is displayed at the same time.

Class A{public:int A; A (int x): A (x) {}//a& operator= (const a& x) {a = X.A; return *this;}}; Class B:public A{public:int B; b (int x): A (x), B (x) {}b& operator= (const b&); b& b::operator= (const b& x) {if (this = = &x) return *this;//a::operator= (x);//Call the base class's assignment function to write this way static_cast<a &> (*this) = x;//can also be cast to a type and then in the default assignment function of the call base class B = X.b;return *this;}
The same is true for copy constructors, as well as for the members of the base class, as long as the base class is added to the member initialization list.

17. In operator=, check the case of assigning yourself a value.

This is based on efficiency considerations, where the first detection of an assignment is to assign itself a value, and it returns immediately, as the function in 16 writes. In addition to the assignment of its own members in the class, if there is a base class, it is also necessary to call the base class's assignment function, which adds overhead.

Another reason is to ensure correctness. An assignment operator must first dispose of the resources of an object, such as some pointers to the space of the dynamic request, before assigning a value to the general to release these resources, and then point to the new resource (if at the beginning of the assignment, with some temporary pointers to record all the previous pointers to the memory, Then the memory that the temporary pointer points to is freed after the assignment. This is still not possible, because for these pointers such as p, there must be P = new p[] ... To point to a space for a new application, and if it is the same object, where the original pointer is pointing to a new address, and the two are the same, a new temporary pointer is needed to point to the value of the pointer to the assigned object, that is, in order to assign it to itself, a new two pointer must be created for each pointer. It is unscientific to store the original pointer of the object on the left, a raw pointer to the object on the right, which is extremely wasteful and unnecessary, and to prepare a large amount of memory to store the data in advance in order to be able to assign a value to yourself.

So the best solution is to detect whether they are assigned a value, generally using the detection object address is equal, C + + generally adopt this method. In Java, it is a bad practice to detect whether an object is equal, that is, whether all its values are exactly equal, and a better way to determine whether an object is the same object, based on the object's ID.






Effective C + + 11-17

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.