I. instance 1, as shown below:
# Include <iostream>
# Include <string>
Class startable;
Class startableprocess {
Public:
Startableprocess (startable & _ startable): startable (_ startable ){
STD: cout <"construct startableprocess" <STD: Endl;
}
Void run ();
PRIVATE:
Startable & startable;
};
Class startable {
Public:
Startable (): _ process (* This ){
STD: cout <"construct startable" <STD: Endl;
}
Void start (){
STD: cout <"startable: Start ()" <STD: Endl;
_ Process. Run ();
}
Virtual void prerun (){
STD: cout <"startable: prerun ()" <STD: Endl;
Run ();
}
Virtual void run () = 0;
PRIVATE:
Startableprocess_process;
};
Class rtmfpserver: Public startable {
Public:
Rtmfpserver (){
STD: cout <"construct rtmfpserver" <STD: Endl;
}
Void start (){
STD: cout <"rtmfpserver: Start ()" <STD: Endl;
Startable: Start ();
}
Void run (){
STD: cout <"rtmfpserver: Run ()" <STD: Endl;
Handle ();
}
Virtual void handle ();
};
Class server: Public rtmfpserver {
Public:
Void handle (){
STD: cout <"server: handle ()" <STD: Endl;
}
};
Inline void startable: Run (){
STD: cout <"startable: Run ()" <STD: Endl;
Prerun ();
}
Inline void startableprocess: Run (){
STD: cout <"startableprocess: Run ()" <STD: Endl;
Startable. prerun ();
}
Inline void rtmfpserver: handle (){
STD: cout <"rtmfpserver: handle ()" <STD: Endl;
}
Int main (INT argc, char ** argv ){
Server server;
Server. Start ();
Return 0;
}
Ii. Example 2:
# Include <iostream>
# Include <string>
Class rtmfpmanager;
Class rtmfpserver {
Public:
Rtmfpserver (INT cores ){
STD: cout <"construct rtmfpserver, cores =" <cores <STD: Endl;
}
Void start (){
STD: cout <"rtmfpserver: Start ()" <STD: Endl;
Run ();
}
Void run ();
Virtual void manage (){
STD: cout <"rtmfpmanager: Manage ()" <STD: Endl;
}
};
Class rtmfpmanager {
Public:
Rtmfpmanager (rtmfpserver & server): _ server (server ){
STD: cout <"construct rtmfpmanager" <STD: Endl;
Handle ();
}
PRIVATE:
Void handle (){
STD: cout <"rtmfpmanager: handle ()" <STD: Endl;
_ Server. Manage ();
}
Rtmfpserver & _ server;
};
Class linyanwenserver: Public rtmfpserver {
Public:
Linyanwenserver (INT cores): rtmfpserver (cores ){
}
Virtual void manage (){
STD: cout <"linyanwenserver: Manage ()" <STD: Endl;
}
};
Void rtmfpserver: Run (){
STD: cout <"rtmfpserver: Run ()" <STD: Endl;
Rtmfpmanager Manager (* This );
}
Int main ()
{
Int core = 99;
Linyanwenserver server (CORE );
Server. Start ();
Return 0;
}
Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8567685