table of Contents
- static与this
- Constructor into private (singleton)
- cost
- Class Template
- Function template
- Member template
- namespace
- Conversion Functions
- explicit and implicit conversion
- pointer-like classes ---- smart pointers and iterators
- function-like classes ---- functor
- Template specialization
- Partial template specialization
- reference
- new and delete
C ++ object-oriented programming _Part1
C ++ object-oriented programming _Part2
course in addition to the object-oriented, it also added some other things, dealt with here, there is a part of the course involved in C ++ 11, I did not write it, put C ++ 11 section is devoted to the discussion, in addition, here are some parts from hujiese notes, I made some supplements to reduce the workload.
static与this
When no static type of a variable or function, class member functions complex is only a code through this pointer (who calls whom is this address) to call the different objects, this will be a pointer parameter passed in default, such as when you call this function in real, immediate return it is re, but the compiler will be preceded this->, is used to specify the calling object of re. And plus static, and this static variables and functions declared there is no relationship, can not be called static types of variables and functions through this, then what use is it, for example, when different users to store bank deposits, c1, c2 and c3 represent the user's own data, but some data has nothing to do with user data, such as interest rates, the interest rate at this time can be declared as static, you can access all objects (static scope is global). As shown below:
Static data declared in the class, to be outside the definition, the static operation of the static function set_rate m_rate (static variables typically modified by static function), the function calls the static set_rate, there are two ways, as shown above, by calling object when not passing this pointer, which points to note.
Constructor into private (singleton)
The mention in part1 before, Hou Jie teacher to explain more carefully carried out, which is a well-known design patterns (singleton), only to create an object, as in the constructor into private, so can not directly name the object create, create the specific practices in private inside a static type of the object, the role of global, accessible by public static function. You can then call other non-static member function of. The following is the example better wording:
When there is no getInstance calls, static objects are not created, only getInstance call, it will have a static object, such wording better.
cost
cout Why have access to so many different types of variables, here is the reason, until the upper right corner of the image may be a cout ostream class, consistent with the code by the ostream. Then look at the definition of ostream, you can see a lot of remodeling inside ostream <<
operator, various types, which is the reason why the cout can print a variety of different types of data.
Class Template
In consideration of a template class may store different types of data and functions, it does not define the type, for example, occur when the above complex
Function template
Template class template function similar except that the compiler will derivation function, argument, not necessary to specify the type, as long as the comparison of the reconstructed objects <
operator, this design is very reasonable, then the ratio is a function of the size of the writing, design stone people, it is necessary to reload the responsibility of <
the operator to separate the responsibility is very good.
Member template
Members of the template used more in the generic programming, in order to have better scalability, the above figure as an example, is often Tl base class U1, T2 to U2 often is the base class, the following example can be seen:
In this way, as long as the incoming U1 and U2 of the parent class or ancestor class is T1 and T2, it can be realized inheritance and polymorphism of clever use this way, but the reverse is not. Many have used this way in STL:
namespace
The above is written using namespace three. namespace can prevent conflicts, such as in the development of a project, different people may be named the same functions and the same parameters, different people need at this time to establish their own codes of different namespaces, so do not worry even if the function of the same name .
Write test cases can use the namespace, to prevent naming conflicts, it is convenient.
Conversion Functions
Function is converted into another type of object type of object, such as the above Fraction class, its data to the numerator and denominator, and when the double Fraction class operation, will automatically call a double conversion functions (conversion function operator is beginning , followed by the type and then to convert brackets). An example in the following examples, the compiler analysis double d = 4 + f is an integer of 4 process is determined, and then continues to determine f, f observed Providing Double () function and will be Double f () operation, is calculated to give 0.6, and then adding 4, finally resulting double type 4.6. (Generally not change the data conversion function, usually to add const)
explicit and implicit conversion
Figure above defines a class called Fraction, which class overloads the "+" operator, during the operation F + 4, "4" is implicit conversion compiler (the constructor) of Fraction object, then Fraction overloaded "+" operator involved in computing.
As shown above, the increase in double Fraction () function, the two members Fraction division variables, and then converted to double force and returns the result, in the f + 4 during overload compiler error can We made the following analysis:
1, the first 4 are implicitly converted (constructor) of Fraction object, and then calculates by overriding the "+" operator and the "F";
2, f is first converted (conversion function) as a double, and then adding the two double performed;
Two routes are possible, the compiler does not know what kind of execution, resulting in ambiguous error. Here is the solution.
In front of the constructor by adding explicit, is to tell the compiler, do not turn into an int type Fraction, can only be constructed by explicitly. So then execute that statement is that the implementation of two double sum, but because Fraction double conversion transfer function is not implemented, so that error.
The above is an example of the STL vector for
pointer-like classes ---- smart pointers and iterators
c ++ class to build up, the object image may be generated out of a pointer, or a function, as this section is about the pointer.
The above examples are smart pointer, smart pointer is a pointer to the original packaging, imparting more functions, wherein the main memory is automatically manage memory leaks can be seen from the above, there are three smart pointer is syntactically key areas, a first external pointers are saved, T * px corresponding to the previous figure, the pointer will replace the incoming pointer operations related to incoming pointer; the second is overloaded "*" operator, Solutions reference, a return object pointer points; third is overloaded "->" operator, returns a pointer px is corresponding to the FIG. Overload here "->" operator is very interesting, returns px, sp-> method becomes pxmethod, but in reality is not, or px-> method, because the operator "->" comparison is not the same, it would have been "->" means going until method refers to an object or data.
Iterator is also a smart pointer, where there is smart pointer above-mentioned three elements, fonts corresponding to the red and yellow in the figure marked portions:
We will carefully analyze the iterator overloaded "*" and "->" overloaded operators:
Create a list iterator object, list :: iterator ite; list here to save the Foo object, which is defined in the template list T class, operator () returns a ( the Node) .data objects, node is __link_type type, however __link_type is __list_node
function-like classes ---- functor
Can be seen from the figure, each is a functor class overloads the "()" operator, then becomes a "copy function", or a substantial class, but appears to have a property function. In fact, each copy function are integrated in the back of a strange class, shown below, this class is not a manual programmer explicitly declared.
Standard library functor also inherited a strange category:
This class is shown below, but the statement something, there is no actual variable or function, which is discussed in particular in STL.
Template specialization
Refers to the Japanese template of the template to specify a particular data type, so that when calling the specified types of templates, using a specialized version (e.g., the above hash
Partial template specialization
Template specialization there are degrees of, you can specify the type of part, called a partial specialization, partial specializations there are two, one is the partial number, one is the partial range.
The first is a bias in the number of the FIG., Some types of binding, the second partial range of the map is, the original version of the point C can be of any type, especially the following C pointer type, OBJ1 is a class C configuration, obj2 is a class C <T *> configuration.
reference
reference can be seen as an alias referenced variables.
new and delete
The principles of object-oriented programming _part1 talked about some new and delete, duplicate content no longer talk, supplemental content about the overloaded new and delete. There is a very good blog articles, you can also refer to: https://www.jianshu.com/p/d2d5cdd7aa1d
The above example is overloaded new and delete, and the like operator overloading, operator first, and then later introduces new, new and delete two formats, based on a new individual, and based on the new array, so there are two written above. There are in parentheses are based on the array. Overloading new function parameters based on the object size to be allocated is derived. Overloaded delete function parameter is the address of the object to be released. The image above is a direct overloaded overloaded global new and delete, a great impact, a memory leak is very dangerous.
Figure above is overloaded individual members of the new and delete operators, is more suitable. Accordingly, new and delete operators performing the left, will be mapped to the right of the step.
Accordingly, there are overloaded based array object. The above distribution of N Foo object, see sizeof (Foo) * N + 4, where N 4 is stored in this number, so that the time to know how many times the constructor and destructor to be called.
The following is a more particular case, for the reload class Foo new and delete operators, there are three data Foo, 12 bytes, to be noted that the reconstructed image above the main classes of how to write new and delete, Additionally, the :: plus new front, it represents the mandatory use of global operators new and delete.
Call statement left, the middle is the output, the above is no virtual functions, virtual functions are the following, for the array based on the new and delete, will be more than four bytes used to store length information, is configured from the bottom destructor from bottom to top.
For global, the process is similar.
It allows you to override placement new and delete. The following are examples:
It defines four operator new function. The first two standard library has to offer.
The corresponding operator delete operations on page slide.
Examples of the standard library, multi-allocate memory used for reference count.
Object-oriented portion ends.