Symbian C ++Wonderful use of pure virtual classes in program development
When talking about Symbian program development, I was most impressed by the pure virtual class applications, which are almost everywhere. However, if you are developing an MFC application, however, it is difficult to see the use of pure virtual classes. At first I thought that pure virtual classes were used because embedded programs had a large number of asynchronous message responses, when I learned to use pure virtual classes to design interfaces for my own programs, I realized how wonderful it is to use pure virtual classes!
The first time I encountered a pure virtual function, I saw a class about time out trigger control. This class is very simple. When it times out, it triggers a processing method, let's take a look at how this class is designed:
First, design a pure virtual function to implement the timeout processing method. When a class requires timeout processing, as long as it inherits the class and implements the processing method for the pure virtual function of the class, the timeout processing method can be implemented in this class:
Class mtimeoutnotify
{
Public:
Virtual void timerexpired () = 0;
};
The following is a class that inherits the timer. This class carries an object of the above pure virtual class (inotify object). When timeout occurs, it will call the method of this object, thus, a timeout processing mechanism is established:
Class ctimeouttimer: Public ctimer
{
Public:
Static ctimeouttimer * newl (const tint priority ority, mtimeoutnotify & atimeoutnotify );
Static ctimeouttimer * newlc (const tint priority ority, mtimeoutnotify & atimeoutnotify );
~ Ctimeouttimer ();
Protected: // from ctimer
Virtual void runl ();
PRIVATE:
Ctimeouttimer (const tint priority ority, mtimeoutnotify & atimeoutnotify );
Void constructl ();
PRIVATE:
// Member variables
Mtimeoutnotify & inotify;
};
The following is the construction method of the Timer class. From this constructor, we can see that the object of the pure virtual class needs to be input when the class is instantiated:
Ctimeouttimer: ctimeouttimer (const tint priority ority, mtimeoutnotify & atimeoutnotify)
: Ctimer (Priority ority), inotify (atimeoutnotify)
{
}
When creating a class that applies the timer, we only need to inherit the pure virtual class and reload the methods in the pure virtual class, and then pass the object of the application class as a parameter, if there is an application Class A that requires a timeout mechanism, Class A has a ctimeouttimer member object B, and Class A inherits the virtual class ctimeouttimer, and B can be instantiated as follows:
B = new ctimerouttimer (0, this );
The following function implements the timeout callback method:
Void ctimeouttimer: runl ()
{
Inotify. timerexpired ();
}
In the Symbian C ++ SDK, almost all classes with asynchronous message responses must have a pure virtual class, because mobile phones are embedded devices and many messages are asynchronous messages due to limited resources, therefore, applications with pure virtual classes can be seen everywhere. However, pure virtual classes are not limited to asynchronous message methods. They can also be applied to the module interface design that requires callback methods, using pure virtual classes to create corresponding interfaces has the following benefits:
1. The module interface is clearer;
2. The module design is more independent and flexible;
3. Less coupling between modules.
There is another benefit that I cannot tell. It will make you feel how wonderful this callback processing method is! I am deeply impressed that C ++ can be used to design a program framework with a very good structure. Unlike the C language, I always feel like a piece of cake (of course, C language programs can also have good frameworks, but it is rare ).
I always think that programming will also enjoy the aesthetic of Art. Of course, the premise is that the program should be well written.