Houtie "C++/OOP/GP/DP" lecture experience (Turn)

Source: Internet
Author: User
Tags object model
Houtie "C++/OOP/GP/DP" lectures

——— Author: Naven

I am very glad Houtie teacher came to the company again, give us four days very vivid technical lectures, benefit, now I briefly introduce my learning experience, and share with you. This lecture mainly concentrates on "the C++/OOP/GP/DP" the topic, for has some programming foundation engineer, to some commonly used code and the design did very easy to understand the analysis, very helpful. Of course, a more in-depth understanding of the need to combine a variety of technical classics to learn, I combined my understanding and my own learning and development experience to introduce c++/oo/template and design pattern, taking into account the nature of the lecture, I do not directly describe the content of this lecture, welcome to criticize the J

Houtie Teacher's lecture is basically about his years in the field of C + + research results, most of it can be read on his books and websites, but given the booming of software technology in recent years, such as the wider use of design pattern, there are a lot of ideas, basically a more pan based level, Combined with actual code and application, it is very beneficial to the real project development. I'll go through the topic in generalities.

Synthesis (composition) and inheritance (inheritance) relationships in object-oriented

There are usually two ways to extend the functionality of a class, one that is familiar to you (inheritance), the other is compositing (composition), a lot of beginner OO (object-oriented) and there are some experiences that can easily confuse this distinction, in fact very simple, inheritance is to solve Is-a problems, while synthesis is the solution to has-a problems. For example, a bird has two wings, is a synthesis, the bird is a kind of birds, is inherited, the design of a "bird" class, it inherits from the "birds", it has "flying" characteristics, but to use the synthetic method "contains" a "wing" of the class to have a real "fly" function.

Although these two definitions are very simple, in fact, many people have made mistakes, including the Java class library designers, they have the Properties directly "inherited" from Hashtable, where in fact should be "synthesized."

When it comes to synthesis, it should be said that the aggregation (Aggregation), which describes the relationship between the whole and the local, the synthesis is actually a "strong" aggregation, which has the same life cycle as the local, "holds" the local "object", and aggregation is only "hold" a local "pointer". For example, people and brains are synthetic, and cars and engines are aggregates, and modified cars can be replaced with better engines, and people's heads are no good (so far:)

Aggregation is represented in UML by a hollow Prism arrow, which is represented by a solid Prism arrow.

There is also a relationship called a delegate (delegation), which is a way to make compositing (composition) as powerful as the multiplexing capability (inheritance). (A way of making composition as powerful for reuse as inheritance [Lie86, JZ91]) in a delegate, two objects are associated when processing a request: A received object delegate operation to its delegate Object. This is similar to the subclass (subclass) Deferred request (deferring requests) to its parent class. But in inheritance, an inherited operation (inherited operation) can often refer to the received object through the this member variable. In order to achieve the same effect in a delegate, the receiver transmits itself to its principal so that the entrusted operation can refer to the recipient.

Again, inheritance (inheritance), which inherits all of the base class (Base-class) all (including private), so if you want to implement a new class and just want to inherit a part, use the synthetic (composition) inheritance. Or, further, if you want to transform a class and want to transform some interfaces (interface), it is also recommended to use compositing, by adjusting the methods of the inner object, and not using virtual functions (virtual function). This is very much in line with the most basic OCP design principles (open-closed principle, open and closed principles).

class constructs (constructor) and destructors (destructor)

The construction and deconstruction of classes are the most basic knowledge, and the object generation and destruction of any class must have these two steps, but how they work, how the compiler makes default ctor and Dtor, and it is estimated that less attention is paid to it.

The generation of objects of a class, in turn, begins with its innermost class, and the same class is constructed sequentially in the order defined by the inner class member. The process of destroying a class object is reversed. The constructor of the base class is invoked before the user-defined ctor, and the base class is dtor after the user-defined. Familiar with these processes, it is very useful to design a good class library, and is not prone to memory leaks and resource depletion problems. Here's an example that's easier to understand:

Class A {public:a (); ~a ();};

Class B {public:b (); ~b ();};

Class C {public:c (); ~c ();};

Class D:public A, B {

Public:d () {init ();} ~d () {release ();}

Private:void init (); void release (); c C;

};

The ctor construction process for class D in the definition above is as follows:

A::a ();

B::b ();

C.c::c ();

D::init ();

The dtor process of class D is as follows:

D::release ();

C.c::~c ();

B::~b ();

A::~a ();

More complex inheritance relationships and multiple inheritance constructs and destructors are similar, and interested people can write program tests:

There is also a question, when the compiler will automatically generate ctor and dtor, and how to produce it?

It's very simple, when you don't write the default constructor (default constructor) and default destructor (destructor), the compiler automatically generates one for you, in other words, any class has constructors and destructors, Although there are times when you do nothing, a copy constructor (copy ctor) is also generated automatically. But how it happens is related to the members of your class. If the members are all native types, and if the class members are all native types, ctor will only give an initial value, just like the initialization of the normal variable definition, dtor do nothing, and copy ctor will copy the object using memory Copy (memcpy). If a member contains one or more class members, and at least one of the class member definitions has a default constructor method, the resulting ctor calls each member's ctor in turn. Dtor and Copy-ctor produce similar methods. (see "Inside the C + + Object Model")

Polymorphic (polymorphism) and virtual functions (virtual function)

Polymorphism is the basic characteristics of object-oriented, C + + is through virtual keywords to provide, it is by adding VTBL virtual function table in class objects to achieve, this is believed that most programmers are very clear, but how to do polymorphic function estimates do not know much. To learn more, please also read the Inside the C + + Object Model book, the following simple introduction to the principle.

General compilation will give a virtual function of the class head (some of the compiler will also put to the bottom, such as VC) to add a member vptr pointer, point to a VTBL virtual functions table, a fixed-length array, size is all the number of virtual functions plus 1. The virtual function pointer starts with VTBL[1] and points to a specific function implementation in the order defined. If the subclass defines a function with virtual in the parent class, the corresponding pointer vtbl the function implementation of the subclass, otherwise it points to the implementation of the parent class. In addition, Vtbl[0] is a different use for storing type information for dynamic_cast purposes.

Still take the example above, for example, how the following code compiler handles:

A *p = new D (); Up-cast

P->vfunc1 (); The compiler converts to the following code

(* (P->VPTR)) N (p); n is the fixed number determined at compile time, that is, the corresponding virtual function

Location

One thing to keep in mind is that the base class always has virtual destructor. Because when the following actions

Delete p;

If a and B destructors are not virtual functions, the dtor of subclass D is not invoked, which can cause serious problems such as memory leaks or resources not being released. If you add virtual Dtor to base class, you will automatically invoke subclass's dtor due to polymorphic features, and then you will call the dtor of each base class in turn, so there is no problem.

C + + template and STL containers

C + + template-template technology is the implementation of generic programming technology, enabling the writing of a code can be applied to similar uses in different places. Template technology In fact, the principle is relatively simple, but the use is still more complex, look at the STL source to know, if you do not believe, and then look at Boost code good, will make you dizzy. Hou Jie teachers to explain this technology is very clear and understandable, but also specific analysis of the STL in the operation of the major components of the principle, I do not speak here, the basic source of the analysis, please read Hou Jie teacher's "STL Source Analysis," a book.

There is a section of code that explains how templates implement function class in STL (classes that implement functional functions, in stl_functions.h)

Template <class _operation>

Class binder1st

: Public Unary_function<typename _operation::second_argument_type,

TypeName _operation::result_type> {

Protected

_operation op;

TypeName _operation::first_argument_type value;

Public

binder1st (const _operation& __x,

Const TypeName _operation::first_argument_type& __y)

: OP (__x), value (__y) {}

TypeName _operation::result_type

Operator () (const typename _operation::second_argument_type& __x) const {

Return op (value, __x);

}

};

Someone proposed above _operation op; Why not defined as a reference, such as _operation &op; it. My idea is as follows, because the construction method is

binder1st (const _operation& __x,//Here is the const type

Const TypeName _operation::first_argument_ty

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.