Implementation of C language Polymorphism

Source: Internet
Author: User

I believe many people have read the design model books. What do you know? Bridge, proxy, and factory are all based on abstract classes. Abstract objects are the core of this process.

In fact, I think the core issue of framework-based programming is abstraction. using abstract objects to build the main framework of a program is a common idea of object-oriented programming. Building a skeleton with abstraction, coupled with polymorphism, forms a complete program. Because the C ++ language implements inheritance and polymorphism, this programming concept is used? It is quite common in C ++. It can be said that virtual (polymorphism) is the soul of VC.
 
However, we almost forget this polymorphism when using the C language. I often hear the predecessors say, class? Polymorphism? We use C. Forget this. Unfortunately, I am a stubborn person. Why not use such a good thing. I'm glad to see the polymorphism in C in some recent pure C code! Next, let me hear about it.
 
1. What is the interface in VC?
Interface: The Chinese explanation is the interface. In fact, it represents a pure virtual class. But what I want to say is that the interface in VC is actually struct. You can find the definition of the interface, and you can find the macro definition as follows:
# Ifndef Interface
# Define interface struct
# Endif
Moreover, in VC, if a class has a virtual function, there will be a vtable in the class, which is actually a list of virtual functions. In fact, C ++ is developed from C, but it supports many new features at the language level. In C, we can also use such features, the premise is that we have to implement it on our own.
 
2. How to Implement pure virtual class in C (I call it pure virtual structure)
Compared with the previous one, I believe everyone has become more open. You can use the struct combination function pointer to implement pure virtual classes.
Example: typedef struct {
Void (* foo1 )();
Char (* foo2 )();
Char * (* foo3) (char * st );
} Myvirtualinterface;

In this case, we should use the bridge mode in the main framework. (Our main class is domyact, and the specific implementation class of the interface is act1 and Act2.) I will introduce these "classes" in sequence ". (The class in C is described above. Here we change it to an early array method)
 
Main class domyact: The main class contains myvirtualinterface * m_pinterface; the main class has the following functions:
Domyact_setinterface (myvirtualinterface * pinterface)
{
M_pinter pinterface;
}
Domyact_do ()
{
If (m_pinter return;
M_pinterface-> foo1 ();
C = m_pinterface-> foo2 ();
}
Sub-class act1: implements the virtual structure, including myvirtualinterface st [Max]; has the following functions:
Myvirtualinterface * act1_creatinterface ()
{
Index = findvalid () // Object pool or use malloc! Should be applied outside and instantiated
If (Index =-1) return NULL;
St [Index]. foo1 = act1_foo1; // act1_foo1 must be implemented in the following
St [Index]. foo2 = act1_foo2;
St [Index]. foo3 = act1_foo3;
Return & St [Index];
}
Sub-class Act2 is the same as above.
 
In Main, assume there is an object list. If the myvirtualinterface pointer is stored in the list, the following are available:
If (P = act1_creatinterface ())! = NULL)
List_addobject (& list, P); // Add all
 
While (P = list_getobject ()){
Domyact_setinterface (p); // use the interface to replace the switch case of the original large space
Domyact_do (); // ignore the specific action, just do it
}
 
Free all.
In Microsystems, such as embedded systems, the object pool technology is usually used. In this case, you do not need to consider the release issue (the object pool has no space in advance, and attach is used, apply for an array in a function and temporarily allocate space for the object pool. In this way, the function ends and the Object pool is released)
 
However, in a PC environment, because of the large program scale and more importantly some special requirements, the object lifecycle must be extended beyond the applied function body, you have to use malloc. In fact, even in C ++, automatic release of new objects is always a headache. The new standard introduces smart pointers. However, for me personally, I think it is untrustworthy to give the whole problem of memory release to the machine, and it can only achieve the optimal performance.
 
Do you know how difficult it is to design the Java garbage collection algorithm? The real world is complex. without a prior condition, it is necessary to obtain accurate results and their difficulties. So I said that programmers should keep free at all times. The robustness and self-defense of the program will be described in another article.
 
3. Degradation of pure virtual Structures
Next let's take a look at what is there only one function in struct? At this time, if we do not use struct, what is the use of function pointers? We found that this degrades to the use of common function pointers.
 
So sometimes I think object-oriented is just a form, not a technology. It is a viewpoint, not an algorithm. However, just like the relationship between carbon, graphite and diamond, although the molecular formula is C, the composition method is different and the performance is completely different!
Sometimes, we are often troubled by trivial things in programming, and thus shift away from the center of gravity. In fact, the evolutionary features of programs are very important. It is possible that, for the first time, it was unsuccessful, but as long as it could evolve, it could develop.
 
4. Advanced-class structure tree. The parent class is not a pure virtual class.
The above is just about the situation where the parent class is purely virtual (Object-Oriented we suggest that the base classes of all classes start with pure virtual classes ), so what if the parent class is not a pure virtual structure when there are many classes. Hey, in fact, the implementation in C is much simpler than that in C ++. Because the functions in C are scattered.
 
Macro definition is a good method here: for example, two classes of 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 must be implemented in the following
St [Index]. foo2 = actbyother1_foo2;
St [Index]. foo3 = actbyother1_foo3;
Return & St [Index];
}
 
# Define actbyother1_foo1 act1_foo1 // This is the inheritance
Actbyother1_foo2 () {}// you can modify its implementation.
Actbyother1_dobyother () {}// Of course, you can add a new implementation.

 

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.