Effective C + + constructor destructor assignment operator

Source: Internet
Author: User

Turn from: http://www.kuqin.com/language/20120723/323076.html

In the process of reading "effective C + +" This book, I have countless times to send a sigh, this his mother wrote too good, a sentence on the nail, directly to the point. So I decided to write 5 blogs about the content of this book, and I think it is worth remembering whether you understand these terms or not. The following index corresponds to the chapters in the book.

11: If memory is dynamically configured within class, declare a copy constructor and a assignment operator for this class

12: In the constructor as far as possible initialization action to replace assignment work

The Members initialization order in the 13:initialization list should be the same as the order in which they are declared within the class

14: Total base class has virtual constructor

15: Make operator = return *this's Reference

16: Assigning values to all data member in operator=

17: In operator = check whether you assigned to yourself

11: If memory is dynamically configured within class, declare a copy constructor and a assignment operator for this class.

The default copy constructor and operator= do not assign a value to each data member one by one of the class, but rather a simple reference that points the object on the left to the object referred to by the right object. No more action, if this class is dynamically allocated memory, such as the left object would point to a memory a, now the left object to the right object refers to the memory B, and no other object to memory a, because it is dynamically allocated, do not recycle, so there is a memory leak, And there are two objects pointing to the same memory, if one of the objects is out of its scope, then its destructor will be invoked automatically, and its dynamically allocated memory will be reclaimed, and now another object is pointing to a piece of memory that has been reclaimed, and as soon as one calls the object's data, an unknown exception will occur, and it is worth mentioning , even if an object does not point to any address or the address it refers to has been recycled, it will not have any problems when it calls its method, as long as its method does not use its data member (which certainly does not exist), because the memory of the method is allocated with the object type, When an object is instantiated, no memory is allocated for the method, and memory is allocated only for data member and some other pointers, such as pointers to the parent class, pointers to virtual tables, and so on.

As described in 11, if you do not declare those two methods, there is also a problem with the method invocation, when the object is invoked as a value, and a temporary variable is generated that refers to the object when the method executes, the temporary variable exceeds its scope, and the destructor is invoked, The object was destroyed in this way. So you have to follow this rule.

12: In the constructor as far as possible initialization action to replace assignment work

The construction of an object is divided into two phases:

1:data member is initialized

2: The invoked constructor executes

If you assign a value to data member one by one in a constructor, you first call the constructor of data member, and if you do not assign an initial value to data member, the default constructor is invoked, and if you assign the initial value to the copy constructor, But my compiler does not allow data member to assign an initial value when it is defined, then call the default constructor, and call operator = When you assign a value to data member within the constructor. The equivalent of calling a constructor and a operator =, and initialization calling only one copy constructor, because the data member was assigned a value when the data member was initialized, You don't have to assign a value to data member in the constructor, and you often encounter questions like: A data member gives him an initial value when defined, and assigns another value to the constructor, what is the value of this data member now? There is also a data member in base class that is assigned multiple values, and then the question is how much it will last. As long as you remember that the constructor of the parent class executes before the constructor of the subclass, the initialization parameter executes before the constructor.

In a word. Initialization efficiency is more efficient than assigning values in constructors, and if data member is large and needs to be initialized to the same value, and the efficiency is not that important, you can assign a value in the constructor using the even equation, which is clear. Efficiency is not always in the first place, and the readability of the code is also important. The 82 law Remember, I used to write an if, in the code review was criticized, the reason is 1 million visits will affect the efficiency, in two companies have encountered this situation, what is the reason, millions access will affect efficiency.

The Members initialization order in the 13:initialization list should be the same as the order in which they are declared within the class

Sometimes the initialization of data member depends on other data member, so the initialization order of data member must be clarified. The initialization order of data member is independent of the order in which they are defined, and the data member defined first is initialized in the initialization, initialization in destructor, Initialize the data member after destructor, so the base data member is then destructor, which is the same as the variables defined first in the stack. If a class inherits more than one class, the initialization order of base data member is determined by the sequence of inheritance, first initialized.

14: Total base class has virtual constructor

In an inheritance relationship, is the method that invokes the parent class or the child class. This dynamic implementation is determined by the virtual function, the class containing the virtual function has a virtual function table, if the method in the parent class is virtual, if no subclasses do not overwrite this method, then it is directly inherited. Regardless of whether the subclass or the parent class calls this method produces the same result, if the subclass has overridden this method, then the subclass's virtual table is the address of the subclass method, if a parent class pointer points to a subclass, and if the method quilt is overridden, then the method called is the subclass method. If it is not overridden then it is the method that invokes the parent class. If the method of the parent class is not virtual and the subclass has the same method, then the method of the parent class is not overwritten by the quilt, that is, the parent class's pointer to the subclass will not be a method of a subclass, but rather a method of the parent class. The same destructor of the parent class is not virtual, so the unknown will occur when you delete the pointer, and the constructor is not invoked anyway, so when you decide to make a class A parent, let his destructor be virtual.

But there is no need to make the destructor of each class A virtual one, because classes with virtual methods have a pointer to virtual table, which makes the object larger, and if the object is inherently less likely to be doubled in cost. It is only when the class contains at least one virtual method that his destructor becomes virtual.

15: Make operator = return *this's Reference

First look at an equation: (a=b) =c, in order to achieve this continuous equation, operator= definitely cannot return void, you can assign a value to *void, but you cannot assign a value to void, and the operator= return way cannot be by value, if so, (a=b) Returns a copy of A or B (the correct way should be the reference of a), let the value of C be assigned to this copy, while the value of a and B has not changed, which is certainly not what we want, in order not to allow this copy to occur, the return value must be a reference to the way, You can return with a pointer or a reference, the pointer must add a * trouble, so it is returned in a reference way, whether to return a reference or a reference to B, is undoubtedly a, if it is B, then the order of execution is this, first assign B to a, Then assign the value of C to B, and the assignment is to assign the value on the right to the return value on the left. This is not what we want, what I want is actually to assign B to a and then assign C to a, and of course it's definitely not a qualified programmer to write this equation. So operator= must return the reference of the left object. I've been wondering why this is a pointer type instead of a reference. Because we must add return *this to the last sentence of the operator= method;

16: Assigning values to all data member in operator=

There is no doubt that if some of the data member is not assigned, then the assigned object is not disabled. Sometimes we forget to assign a value to the class when we add data to it, or we forget to assign the data member of the parent class to the operator= of the operator=. Of course these are not the point, do not remember is not a problem, the nature of the error is known, for the parent class data member only need to add the operator= of the subclass base::operator= (this), if the compiler does not support the call base of the operator= , you can do type conversion ah,static_case<&base> (*this) =derived; I found that in type conversion, the type converted to is usually a pointer or a reference, especially when the conversion type is on the left, it must not be by Value, you will create a temporary variable and then assign a value to the temporary variable, of course, not what you want. When you assign a value to a pointer type of data member, remember that you assign a value to the object that the pointer refers to, instead of assigning a pointer to it, and if you let data member point to that point, a memory leak may occur that some addresses will not arrive.

17: In operator = check whether you assigned to yourself

Before assigning a value to an object, the first to confirm that this object is dynamically configured memory, if the dynamic configuration of memory, the first to reclaim this block of memory, otherwise, when the object is assigned to the other address will appear after the memory leak, of course, can not find it dynamically configured memory to the first recall it, Because you may appear to give yourself to the situation, you can not because you assign yourself to yourself after the inexplicable was recycled it, so in operator= Chinese medicine check whether they assigned to themselves.

Effective C + + series:

Design and declaration of effective C + + classes and functions

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.