effective c ++ reading Summary (1)

Terms 1 c ++ is a language federal
c ++ programming language is divided into four parts Fundamentally, its use of language features and details are also available from four parts:
1. c ++ c compatible
2. c ++ is an object-oriented programming language
3. c ++ template programming introduced
4. c ++ has std library
C ++ makes four criteria, especially multi-depth understanding and if not, then it is difficult to use.
One typical example is relevant in c language int, int * value of transmitted faster than normally passed by reference, and reference is transmitted normally to the class object faster in terms of reference passed (OOP), the template programming the same is true under, because the next programming template can not determine what type of data is, it is recommended to use a reference parameter passing, but the introduction of iterators in std library, etc. due to achieve similar and therefore more likely to pass a reference pointer under normal circumstances.
Article 2 try to use the compiler preprocessor give up
The reason is simple: the preprocessor without compilation process, so there will not be problems prompted at compile time, can easily lead to errors. A typical try to use const, enum, instead of inline #define, #define macro in use is simply necessary to define a function expression or a constant expression, const replaced with constant expressions, function expressions modified by the keyword inline function instead.
const #define macro define variables in place, so that the amount is defined as seen by the compiler, the compiler so that an error can be compiled in accordance with information related to the results or warnings, in order to better macro exhibit similar characteristics, we enum usually required, since the amount #define macro can not be taken out does not take up memory address space, and can be taken const variable addresses corresponding memory space occupied, enum defined enumerated type with similar #define characteristics, so it is appropriate to place #define enumeration type.
In addition I believe that c ++ 11 introduced exprconst better keyword instead of two, because the exprconst modified variable at compile time was calculated, and was only const modified variables need to be defined at run time. inline function similarly modified at compile time are expanded, and exhibited #define macro expansion have similarities, but #define preprocessor is being deployed in phases, and is deployed inline at compile time, and the inline code is easier to analyze, as defined function is well known #define is very error-prone.
Article 3 to make use const
All modifications of the same amount, so that compile more help you analyze the code uses const. Const modified function both as a whole can be modified variable speed closed when a member function of a class is generally modified variable the variable can be any variable, such that the core is const effect or function of a variable becomes fixed, when the modified member he assured the members of a function to change the function does not change any class member variables (except a case)
In the above-described type, the compiler does not return char & given, but this is dangerous, because it results in a call [] operator to change the internal member variable pText after return values, as follows:
In addition to the scope of this provision also provides const and non-const versions of overloaded functions belong in the same class, but the operation because the const and non-const version member function carried out by a lot of similarities so we can use non-const version call the const version member function, but must be displayed twice converted as follows:
Therefore the two member functions to reduce maintenance costs and reduce the degree of redundancy codes.
Article 4 to ensure that the object before calling the object has been initialized
This necessity is not saying much, because the nature of integration and more in c ++ (see Item 1) so some objects are not attached to an initial value (performance in C), and some objects are given initial (non-C performance, such as vector, etc.), for uniformity, all objects are typically the same initial value, wherein the initial value in the initialization table constructor uniform, attention should first be noted that the configuration of the parent class, order is configured Fathers class constructor and is a member of the order of declaration of variables is constructed must be noted that the order of declaration is configured, no matter what is the object initialization sequence table are needed in this order are sequentially configured.
Also note that for global variables, variables namespace in scope, within the class, static variables within the file concerned, when these variables are in different file its initialization sequence is uncertain (note the different file, the same file should order this point is open to question undetermined ???), so their initialization attention should not be called each other, because you do not know at this time whether the object is initialized to be called!
Improved method is to define the function returns a static local objects (static objects within the function), thus ensuring called object must have been initialized, but also introduces thread-safety issues static under the multi-threaded, this solution reference scheme may singleton design pattern approach, of course, the most direct method is not opened before the main function for a number of other initialization unified thread.
Terms 5 c ++ default generated function
In general, if you do not define the class constructor, c ++ compiler will generate a constructor for you
If you do not define copy constructor class, c ++ compiler defined copy constructor for your (shallow copy of copy)
If you do not define the class assignment operator, c ++ compiler defines your assignment operator (shallow copy of copy)
apart from:
1. When the program does not require a constructor, copy constructor, assignment operator when, i.e., in response to the "if not necessary, do not by entity."
2. When there is a class member variable references (reference objects can only be specified at initialization, can not be changed), a member variable copy constructor (or assignment operator) is deleted, all non-presence of the assignment of the const member variables, etc. when the member variable, c ++ do nothing (not automatically generated default copy constructor or assignment operator).
3. parent generation function can not be generated automatically (including a constructor, a copy constructor, assignment operator), for example, in the parent class constructor, copy constructor or assignment operator declared private and the like.
Article 6 determines that no delete function
The default generated function can be problematic in some cases, such as copies of such problems need not exist (for example, some of the functional classes), we should remove those generated by default as a function of the face of these classes (default constructor, the default copy constructor, the default assignment operator) in order to avoid unnecessary trouble or reduce the error rate, so that the compiler further help us analyze the code. In typical use c ++ 11 is **** = delete, there are two solutions in c ++ 11 before:
1. The copy constructor is defined as the assignment operator or private and not implemented, so that an error will appear in the linker stage to terminate the program
2. To be able to early (at compile time found) found the problem, or copying copy constructor operator may be defined as private, as defined elsewhere other than the private class when access is impossible, but the friend class can access, so there are still problems, so a better solution is to define a common parent class, the copy constructor or assignment operator in the parent class is defined as private, so that other classes inherit him, which makes the friend class still he can not copy, as follows:
 
Of course, in 11 c ++ delete keyword solve the problem.
Definition of virtual base class destructor polymorphism with Clause 7 to qualitative
Is defined as a virtual base class destructor polymorphic nature, there is no need to define polymorphisms quality class virtual base class functions (in most cases not required)
The reason is that in the multi-class states, appears the following:
As the code above shows that when the calling function getTimeKeeper (), the pointer base class is bound to a different derived class (which led c ++ dynamic polymorphism), when delete the object pointer, will call the object's destructor, but in this case the pointer to the base class, so if the destructor is not defined as a virtual destructor, then the time will only call the destructor for the base class, the derived class for the destructor will not be calls, therefore derived class generated in the memory will not be released, which raises the issue of memory management. If defined as virtual destructor, it first calls the destructor for the derived class, and then calls the base class destructor, complete memory release.
In fact, this occurs lies in the use of virtual table, when the definition of the base class virtual function destructor, then he will be derived class at a base class constructor destructor pointer to derived class virtual function table, Therefore, when the release of naturally derived class virtual function call. But again it raises the question, virtual function tables also need to be stored and the need to take up some memory space, so we say that when the class does not have the multi-state characteristics we do not deliberately defined as the destructor virtual functions, in order to avoid unnecessary memory footprint.
Another point on the issue of virtual destructor, when the destructor is defined as a pure virtual function, the class will be referred to as a virtual base class, then the destructor should still be defined, pure virtual function is still yes It can be defined, and must be defined at the moment! Therefore he was about to be called! When the derived class destructor is bound to call the destructor base class, if not defined so it will inevitably lead to problems. Thus pure virtual destructor may be defined and must be defined!
Do not let the abnormal escape clause 8 destructor
Should define in c ++ when an abnormality occurs when two simultaneously (not processed after a second occurrence of abnormality occurs), such that the trigger std :: terminate program termination or undefined behavior, it is necessary to ensure that after the exception generates an exception is processed, and when an abnormality occurs destructor, two solutions, the first one is in the destructor is determined whether there is an abnormality, the second embodiment is the destructor function call, the function determines whether there is an outer abnormalities, both methods have problems second way: when the class object is stored in the vector vessel, will in turn call the destructor vector stored in the vector if the object is destroyed if the destructor function is abnormal, and the abnormality to be treated, then the exception has not been processed before the destructor all the vector object, if there are 100 objects in the vector, it is essential that 99 the object is no abnormality, if there is a the above object destructor abnormal then the inevitable collapse of the program, and this will be a very high probability, it is necessary to ensure that the destructor is no abnormality Must ensure that abnormal or sub ah internal destructor treated!
9 Terms avoid virtual function call in the constructor and destructor
The reason is that defined in the base class virtual function overrides the virtual function in the base class, when the dynamic binding, we will bind the base class pointer to an object of a derived class, this time due to the constructor first calls the base class constructor then call when calling the constructor of a derived class, so when calling the constructor for the base class virtual function table points to virtual function pointer still points to the derived class virtual function instead of the base class, polymorphism thus such failure (function is not implemented) will inevitably lead to errors, thus avoiding the virtual function call the constructor of the more serious when the base class virtual function is a pure virtual function. as follows:
When the above code is defined Transaction * tra = new SellTransaction (); after the program terminates, because you call a pure virtual function virtual void logTransaction () const = 0; function.
Destructor should not call virtual functions, since: the first is called a derived class destructor destructor derived class, the derived class destructor will usually derived class used in memory freed, then to call the destructor for the base class, when the destructor of the base class virtual function is called, since the virtual function pointer points to the polymorphism of a derived class, the derived class may already have access to released a derived class destructor out of memory space, then it will produce undefined behavior, and cause inconclusive results. Therefore it should not be called a virtual function in the destructor!
Clause 10 * this return value in the assignment operator is defined as the left reference value
This is not certain that they did so for two reasons:
1. In order to complete the operation and the like even
2. std library so that all assignment operators are written
Author: otherwise it? He returned value? Return rvalue references it is clearly not, because the right reference value can not be left as an operational value, such as
int a=b=c=1
Since the calculation formula is bound to the right, and therefore can be seen as a = (b = (c = 1)), = if the operator returns the right value, then c = 1, are not allowed, so the right value is negated
If the return value does not return a reference, then more than a copy constructor, and therefore must be left * this reference value
Article 11 assignment operator of self-replication process
By definition is the assignment operator copy operation between objects, the general method of assignment:
There will be a matter of implementation of the above assignment operator, the two operations when the object is first assignment operator is the same time, this approach will not be used, because the object is in turn calls delete the object in the new Bitmap. The general idea is to solve, after entering the assignment operator function first to determine whether the same object:
This solves the problem, but still there is another problem: When delete pb, we call pb = abnormal (* rhs.pb) when new Bitmap, leading to assignment failure pb, then there will be undefined behavior pb of ( At this point pb has been delete), which is not known at this point where pb. To solve this problem:
The method by first defining Bitmap pointer to the original memory, then new Bitmap, and then the new out of the memory space assigned to pb, all this and then delete the original memory space after a successful, if abnormal new memory space appears, then pb pointer still point yuan memory space, undefined behavior does not occur. Note that this does not achieve the same special treatment to two assigned object method, this is natural because the method can solve such problems. Of course, if you want to consider the limits of efficiency issue, you can also join "card with the test." An alternative to the manner as follows:
Note two things:
(1) Parameters are passed by value rhs
(2) swap (RHS) exchange pointer, swap function is a function without exception, will be described in detail later 25 implementation swap and no abnormality which is why the function (abnormal allowed).
12 in terms of the assignment operator do not forget to copy all the member variables
Easy to forget members include:
Deep and shallow copy copy problem (1) pointer
(2) as a sub-category needs to consider the parent class members to copy
(3) pay particular attention to adding new members in the development process after all of the copy constructor and copy operators need to be revisited.
 

Guess you like

Origin www.cnblogs.com/liumantang/p/11966804.html