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 problem with the depth of the copy, to rewrite the copy constructor. Make it a deep copy, the ability 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 =.

Assignment is very similar to replication, and the default operation is to replicate all members of the class.

Deep copy basic operation is very easy, for pointers. Dynamically request a piece of memory to hold the data that the pointer points to, each pointing to its own piece of memory, not to 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.

Second, the initialization list is more efficient. Because the object is created in two steps: The initialization of the data member and the operation of the action called in the constructor body. Even before assigning a value. The initialization of the data member is performed 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 order in which members are listed in the initialization list is the same as the order in which they are declared in the class.

This is because the order of the initialization list does not affect the order of initialization. The order of initialization is determined by the order in which members are declared in the class. The order is also to make the program appear to initialize in the order of the initialization 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. This will result in 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, or there will be unpredictable consequences. Instead of using a virtual destructor, simply calling the destructor of the base class to remove the derived class object is not possible. It is also impossible to determine the consequences.

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 the object of the virtual function has a virtual pointer pointing to the virtual table. Will waste space to store this meaningless pointer.

The pure virtual function can be added =0 after the virtual function.


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

= number can be connected together. Because of the reason for its return value, the form of the declaration operator= such as the following:

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 right side is the const type.
The function's parameter 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 of its function as a parameter. and cannot be changed by the function.


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

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

When the inheritance is involved. The assignment operation of a derived class must handle the assignment of the base class.

Assuming that the assignment operation of the derived class is overridden, the base class part must be assigned 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. You also want to copy the members of the base class simply by adding the base class 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 test of the assignment is to assign a value to itself and return immediately. As the function in 16 writes.

This is in addition to the assignment of its own members in the class. Suppose you have a base class, and you want to call the base class's assignment function, you add overhead.

Another reason is to ensure correctness. An assignment operator must first dispose of an object's resources. For example, some pointers point to the space for a dynamic requisition. These resources are generally freed before they are assigned, and then point to the new resource (assuming that the assignment starts. Use a few temporary pointers to record the memory that all previous pointers point to, and then release all memory that the temporary pointer points to when the value is assigned. This is still not possible, due to these pointers such as p, must have P = new p[] ... To point to a space for a new application, and assuming the same object, the original pointer is pointed to a new address, and the two are the same, so a new temporary pointer is needed to point to the value of the pointer to the assigned object. That is, in order to assign a value 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 way to solve this is to detect whether or not to assign a value, generally used to detect the object address is equal, C + + generally take this scheme. for Java. The bad thing is to test whether the object is equal. That is, if all of its values are completely equal, a better approach is to infer whether the 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.