Object-oriented high-level programming Supplement

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

1557714511998

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:

1557715259428

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)

1557715824622

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:

1557716265949

When there is no getInstance calls, static objects are not created, only getInstance call, it will have a static object, such wording better.

cost

1557716414284

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

1557716746695

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 , T compiler will replace the template class code to double, creating a new code encounter complex When will the template class code is replaced with T int, creating a code. You can see the template class code is a multi-use, very convenient.

Function template

1557717032223

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

1560412186484

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:

1560413833493

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:

1560413886633

namespace

1557717432386

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.

1560404974745

Conversion Functions

1560404584181

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

1560405283461

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.

1560405362848

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.

1560406299464

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.

1560406833869

The above is an example of the STL vector for The operator [], bool should be returned, but indeed returns of another type, then this should be converted to bool type conversion function, Sure.

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.

1560407154869

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:

1560410852393

We will carefully analyze the iterator overloaded "*" and "->" overloaded operators:

1560411003846

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 * Type, T here is Foo, so the node is __list_node Type, so ( Node) .data is obtained an object type Foo, and & (operator ()) is obtained a final address of the object Foo, Foo i.e. returns a pointer type.

function-like classes ---- functor

1560411429029

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.

1560411583591

Standard library functor also inherited a strange category:

1560411683785

This class is shown below, but the statement something, there is no actual variable or function, which is discussed in particular in STL.

1560411846337

Template specialization

1560419363053

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 , hash , hash ), Rather than the generic version (original version 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.

1560419673831

1560419917708

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.

1560422076723

1560422092804

1560422107865

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

1560761309230

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.

1560761942136

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.

1560762376012

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.

1560773568977

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.

1560773903634

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.

1560774047068

For global, the process is similar.

1560774481410

It allows you to override placement new and delete. The following are examples:

1560774759364

It defines four operator new function. The first two standard library has to offer.

1560775017510

The corresponding operator delete operations on page slide.

1560775583436

Examples of the standard library, multi-allocate memory used for reference count.

Object-oriented portion ends.

Guess you like

Origin www.cnblogs.com/fydeblog/p/11095850.html