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, which is constructed with abstract objects. Program This is the general 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 that some of the recent pure C Code , I saw the polymorphism in C! 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);
}< br> myvirtualinterface;
assume that the bridge mode is used 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 ". (Classes in C are described earlier. Here we use an earlier array method.)
main class domyact: the main class contains myvirtualinterface * m_pinterface. The main class has the following functions:
Domyact_setinterface (myvirtualinterface * pinterface)
{
M_pinterface = pinterface;
}
Domyact_do ()
{
If (m_pinterface = NULL) 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
}< br>
free all