I believe many people have seen the design patterns of the book, What do we experience? Bridge,proxy,factory These design patterns are based on abstract classes. The use of abstract objects is a core here.
In fact, I think one of the core problems of frame programming is abstraction, which is the general idea of object-oriented programming, which uses abstract object to build the main frame of program. Building a skeleton with abstraction, plus polymorphism, forms a complete program. Because the C + + language itself implements inheritance and polymorphism, use this programming philosophy (what does the idea mean?) With a wind, hehe) in C + + is very common phenomenon, can be said virtual (polymorphic) is the soul of VC.
However, we all use the C language to forget this polymorphism. I often hear the elder say, class? Multi-state? We're using C, let's just forget about it. Unfortunately, I am a stubborn person. Such a good thing, why not use it. Very happy, in some of the most recent pure C code, I saw the polymorphism in C! Below and listen to me slowly.
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 in VC interface is actually suct, look for the definition of interface, you can find that there is such a macro definition:
#Ifndef Interface
#define Interface Suct
#endif
And, actually in VC, if a class has virtual function, then the class will have V, which is actually a list of virtual functions. In fact, C + + is from the development of, it is only at the language level to support a lot of new features, in the C language, we can also use such a function, if we have to implement ourselves.
2. How to implement a pure virtual class in C (I call it a pure virtual structure)
Compare the front, I believe we have been enlightened. Pure virtual classes can be implemented using SUCT combination function pointers.
Example:
typedef suct {
void (*FOO1) ();
char (*FOO2) ();
Char* (*foo3) (char* St);
}
Myvirtualinterface;
This assumes that we want to use the bridge pattern in the main frame. (Our main class is domyact, interface specific implementation class is ACT1,ACT2) below I will introduce these "classes" in turn. (C in the "class" in the previous note, here a change, is the use of early array method)
Main class Domyact: The main class contains myvirtualinterface* m_pinterface; The main class has the following function:
Domyact_setinterface (myvirtualinterface* pinterface)
{
M_pinterface= Pinterface;
}
Domyact_do ()
{
if (m_pinterface==null) return;
M_pinterface->foo1 ();
C=m_pinterface->foo2 ();
}
Subclass Act1: Realize virtual structure, contain myvirtualinterface St[max]; There are the following functions:
myvirtualinterface* Act1_creatinterface ()
{
Index=findvalid ()///object pool or use malloc! Should stay outside to apply, instantiate
if (index==-1) return NULL;
St[index]. Foo1=act1_foo1; Act1_foo1 to be implemented in the following detail
St[index]. Foo2=act1_foo2;
St[index]. Foo3=act1_foo3;
return &st [index];
}
Sub-class Act2 ibid.
In main, suppose you have an object list. The myvirtualinterface pointer is stored in the list, and there are:
if ((p= act1_creatinterface ())!= NULL)
List_addobject (&list, p); ADD All
while (P=list_getobject ()) {
Domyact_setinterface (P);//use interface instead of the original Big Switch case
DOMYACT_DO ()//Ignore specific actions, just do it
}
Free All