OO IN C (2): Polymorphism in C

xiaoxiao2021-03-06  64

C language polymorphism

I believe many people have seen books in design patterns. What do you have anything? These design patterns are based on abstract classes. The use of abstract objects is a core here.

In fact, I think a core problem of framework programming is abstraction, the main frame of building a program with an abstract object, which is a general idea for object-oriented programming. Use an abstract to build a skeleton, plus polymorphisms form a complete procedure. Since the C language itself achieves inheritance and polymorphism, use such a programming concept (what is the meaning of the concept? Hehe) is a very common phenomenon in C , you can say that Virtual (polymorphism) is the soul of VC.

However, us use C language, we will make this polymorphism. I often hear the seniors saying, class? Polymorphism? We use C and forget these. Unfortunately, I am a stubborn person. Such a good thing, why don't you use it? Very happy, in some of the recent pure c code, I saw the polymorphism in C! Let's listen to me slowly.

1.

What is INTERFACE in VC?

Interface: Chinese explanation is an interface, in fact it represents a pure virtual class. But what I have to say is that the interface in the VC is actually Struct, find the interface definition, you can find such a macro definition:

#Ifndef interface

#define interface struct

#ENDIF

Moreover, in practice in the VC, if a class has a Virtual function, there will be vtable in the class, which is actually a list of virtual functions. In fact, C has been developed from C, but it supports many new features on the language level. In the C language, we can use such functions, provided that we have to achieve themselves.

2. How to achieve pure empty category in c (I call it a purely structure)

Compared with the front, I believe that everyone has already suddenly turned over. Pure empty class can be implemented using the Struct Combination Function Pointer.

Example: typedef struct {

Void (* foo1) ();

CHAR (* foo2) ();

CHAR * (* foo3) (char * ST);

MyvirtualInterface;

This assumes that we need to use bridge mode in the main frame. (Our primary class is Domyact, the interface specific implementation class is ACT1, ACT2) I will introduce these "classes" in turn. ("Class C" has an explanation in the previous example, which is a way to use an early array)

Main class Domyact: The primary class contains MyVirtualInterface * m_pinterface; the main class has a function:

Domyact_setInterface (MyVirtualInterface * Pinterface)

{

m_pinterface = pinterface;

}

Domyact_do ()

{

IF (m_pinterface == null) return;

m_pinterface-> foo1 ();

C = m_pinterface-> foo2 ();

}

Sub-class ACT1: Improve the virtual structure, contains MyVirtualInterface ST [MAX]; with the following functions:

MyVirtualInterface * act1_creatinterface ()

{

INDEX = FindValid () // Object pool or use Malloc! Should be left outside, instantix IF (INDEX == - 1) Return NULL;

ST [index] .foo1 = act1_foo1; // ACT1_FOO1 To implement the following

ST [Index] .foo2 = act1_foo2;

ST [index] .foo3 = act1_foo3;

Return & ST [INDEX];

}

Sub-class ACT2 is the same.

In Main, it is assumed to have an object list. Storage in List is the MyVirtualInterface pointer, then:

IF ((p = act1_creatinterface ())! = null)

List_addObject (& List, P); // Add All

While (p = list_getobject ()) {

Domyact_setInterface (P); // Use Interface instead of the original large Switch Case

Domyact_do (); // Don't pay attention to what kind of action, Just Do IT

}

Free ALL.

In the microsystem, such as embedded, usually using the object pool technology, this time you don't have to consider the problem (the object pool is not available in advance, using attach, apply for an array in a function and temporarily assign space for the object pool, This end, the object pool is released)

However, in the PC environment, due to the large size of the program, some special requirements are made, so that the life cycle of the object must continue to the function body applied, it has to use Malloc, in fact, even in C , New Automatic release of objects is always a headache problem, and new standards introduce smart pointers. But I personally, I think that the problem is completely handed over to the machine is not trusted, it can only reach the best.

Do you know how difficult it is to design Java garbage collection algorithms? The real world is intricate. If there is no prior condition, it is necessary to get accurate results and its difficulties. So I said that the programmer should always record free, and the robustness and self-defense of the procedure will be described in another article.

3. Grid of a pure virtual structure

Let's take a look at what if there is only one function in Struct? At this time, if we don't use Struct, just use the function pointer? We found that this is used to degenerate into ordinary function pointers.

So, sometimes I think objects are just a form, not a technology. Is a point of view, not an algorithm. However, as the relationship between the carbon, graphite and diamonds, although the molecular formula is C, but the composition method is different, the performance is completely different!

Sometimes, we are often worried about the trivial things in programming, but deviate from the center of gravity, and the performance of the process evolution is important. It is possible, the first time is unsuccessful, but as long as it can evolve, it can develop.

4. Advanced - class structure tree, parent class is not a pure category

The front is only the case where the parent class is a purely structure (the object-oriented suggestion is that all class base classes start from pure virtual classes), then the parent class is not pureness when the class level is more What should I do? Hey, in fact, the implementation of C is much simpler than C . Because each function of C is dispersed.

It is a good way to use macro here: such as two class ACT1, Actbyother1 "inherit" act1: myvirtualinterface * actbyother1_creatinterface ()

{

INDEX = FindValid () // Object pool or use Malloc

IF (index == - 1) Return NULL;

ST [index] .foo1 = actbyother1_foo1; // act1_foo1 to implement the following

ST [Index] .foo2 = actbyother1_foo2;

ST [index] .foo3 = actbyother1_foo3;

Return & ST [INDEX];

}

#Define actbyother1_foo1 act1_foo1 // This is inheriting 嘿嘿

ActByother1_foo2 () {} // can modify its implementation

Actbyother1_dobyother () {} // can of course add new implementation

5. Example - You can see the source code of H264, where naltool is such a pure virtual structure.

转载请注明原文地址:https://www.9cbs.com/read-112462.html

New Post(0)