Object-oriented language C 2

Transfer from: http: //hi.baidu.com/todaygoodhujun/blog/item/7b3b7cecf4c716d12f2e2132.html

 

 Polymorphism of the C language to achieve
       I believe many people have seen design patterns book, do you have any experience? Bridge, Proxy, Factory design patterns are based on abstract class. Abstract objects is a core here.
      
       In fact, I think the programming framework of a core problem is abstract, body building framework program of the abstract object, which is generally thought of object-oriented programming. Building skeleton abstract, polymorphic coupled to form a complete program. Since the C ++ language itself implements inheritance and polymorphism, the use of such programming concepts (ideas do you mean? With the wind, hehe) is a very common phenomenon in C ++, can be said to Virtual (polymorphism) is a VC soul.
 
       However, using the C language we almost put this polymorphism forgot to be smooth. I often heard the older generation that class? Polymorphic? We use C, these forget. Unfortunately, I'm a stubborn person. Such a good thing, why not do. Very pleased, in some recent pure C code, I saw the C polymorphism! Below listen to me slowly come.
 
1. VC is what the Interface
       Interface: Chinese explanation is the interface, in fact, it represents a pure virtual class. But I have to say that, in the VC Interface is actually a struct, find Interface definition, you can find this macro definition:
       #ifndef Interface
              #define Interface struct
       #endif
And, in fact, VC, if a class virtual functions there, the class which the vtable will, it is actually a list of virtual function. In fact C ++ evolved from C, but it is at the language level support for many new features in the C language, we can also use this function, the premise is that we have to achieve themselves.
 
2. C how to achievePure virtual class (I call it pure virtual structure)
       relatively earlier, I believe we suddenly see the light. Struct using a combination of function pointers can be realized pure virtual class.
Examples: typedef struct {
                     void (* Foo1) ();
                     char (* Foo2) ();
                     char * (* Foo3) (char * ST);
              } MyVirtualInterface;
      
       so suppose we want to use the body frame in a bridge mode. (Our main class is DoMyAct, the interface implementation class is Act1, Act2) Now I will turn introduce these "classes." (C in "class" in front of description, here a change, using the earlier array approach)
 
main class DoMyAct: main class contains MyVirtualInterface * m_pInterface; master class has the function:
                            DoMyAct_SetInterface (MyVirtualInterface * pInterface)
                            {
                                   = pInterface m_pInterface;
                            }
                            DoMyAct_Do ()
                            {
                                   IF (m_pInterface == NULL) return;
                                   m_pInterface-> Foo1 ();
                                   C = m_pInterface-> Foo2 ();
                            }
subclasses Act1: implement virtual structure, comprising MyVirtualInterface st [MAX]; have the following functions:
                    MyVirtualInterface Act1_CreatInterface * ( )
                     {
                            index = FindValid () // target pool or the use Malloc! Should stay out of the application, examples of
                            IF (index == -. 1) return NULL;
                            St [index] = .Foo1 Act1_Foo1; Act1_Foo1 be embodied in the following //
                            St [index] = .Foo2 Act1_Foo2;
                            ST [index] = .Foo3 Act1_Foo3;
                            the Return & ST [index];
                     }
subclass Act2 supra.
 
In the main, it is assumed there is an object List. List MyVirtualInterface is stored in the pointer, there are:
       IF (! (P = Act1_CreatInterface ()) = NULL)
       List_AddObject (& List, P); // All the Add
 
       the While (P = List_GetObject ()) {
              DoMyAct_SetInterface (P); // use Interface instead of the original great lengths Case Switch
              DoMyAct_Do (); // do not care what kind of concrete action, the Just do IT
       }
 
       the FREE ALL.
       In the micro system inside, such as embedded, object pooling technique commonly used, this time can not consider the problem (not previously object pool space, use Attach release, apply an array of functions and objects in a temporary pool allocation for the space, this function ends, object pooling on the release)
 
       But in Pc environment, due to the relatively large size of the program, more importantly, some special requirements, making the life of the object must continue to function outside the body of the application, you have to use malloc, in fact, even in C ++, new automatically release the object is always a headache problem, the new standard introduces smart pointer. But me personally, I think the problem will release memory to complete the machine can not be trusted, it can only achieve sub-optimal.
 
       You know how difficult it is Java's garbage collection algorithm design it? The real world is complex, without prior conditions, in order to get accurate results extremely difficult. So I said, programmers should always keep in mind will be free, robust and self-defense procedures will be covered in another article.
 
3. Degradation of pure virtual structure
       Let us look at what if there is a struct which only functions are? This time if we do not use struct, using only the function pointer, then what is? We found that this degenerates to the ordinary use of the function pointer.
 
       So, sometimes I think object-oriented is a mere formality, not a technology. Is a point of view, rather than an algorithm. However, as the relationship between carbon, graphite and diamonds, even though the molecular formula is C, but the method is not the same composition, performance is completely different!
       Sometimes, we are often bothered by trivial things in programming, but deviated from the center of gravity, in fact, the program can be evolved characteristics is very important. It is possible, for the first time was unsuccessful, but as long as evolution, can develop.
 
4. Advanced - class tree, the parent class is not a pure virtual class
       foregoing merely speaking the case is the parent class is a pure virtual configuration (the object is recommended for all the base class is a pure virtual class from the beginning), so when more class hierarchy, the emergence of the parent class is not how to do it purely imaginary structure. Hey, in fact, implemented in C is simpler than C ++ and more. Since C is dispersed in each function.
 
As used herein, the macro definition is a good way: for example, two classes Act1, ActByOther1 "Inheritance" Act1:
                    MyVirtualInterface ActByOther1_CreatInterface * ()
                     {
                            index = FindValid () // object pooling or using Malloc
                            IF (index == -. 1) NULL return;
                            St [index] = .Foo1 ActByOther1_Foo1; // Act1_Foo1 be embodied in the following
                            St [index] = .Foo2 ActByOther1_Foo2;
                            St [index] = .Foo3 ActByOther1_Foo3;
                            the return & ST [index];
                     }
 
       #define ActByOther1_Foo1 Act1_Foo1 / / this is inherited Hey
       ActByOther1_Foo2 () {} // can modify its realization
    ActByOther1_DoByOther () {} // Add a new course can be achieved slightly

 
5. Example - can be found in the H264 source, wherein NalTool is such a pure virtual structure.

Reproduced in: https: //my.oschina.net/dake/blog/196720

Guess you like

Origin blog.csdn.net/weixin_33831196/article/details/91508088