Then I inherit Server, and created a subclass process_sub_1, then rewrite the method server_poll, expect process_sub_1 when creating objects, the constructor will call me new rewritten process_sub_1: : server_poll,
But in the actual debugging, calls the constructor to create process_sub_1 object, will not use process_sub_1: : server_poll, but still USES the Server: : server_poll
Everybody to help, please look at my code, how to improve
Specific engineering stored in the cloud, using cmake compiled under ubuntu, specific see code readme
A network backup link: https://pan.baidu.com/s/1UhyfD7ORK-4FeOvYHL-AZA
The extracted code: d63q
The definition of the Server class
The class Server {
Public:
Pid_t pid;
EServer service_id;
Char name [MY_PID_NAME_LEN];
//subclass inherits rewrite
Void server_init (void * par, uint32_t len) {}
Void server_poll (void) {}
Uint32_t Server (eServer id, void * par, len);
To the Server ();
The static Server * install (eServer id, void * par, uint32_t len);
The static Server * self;
};
The following is a Server class constructor
Server: Server (eServer id, void * par, uint32_t len) {
Uint8_t I=0;
Pid_t pid=fork ();
This - & gt; Service_id=id;
Bzero (this - & gt; The name, MY_PID_NAME_LEN);
Len=strlen (mServiceName [this - & gt; service_id]);
If (len & gt; MY_PID_NAME_LEN) {
Len=MY_PID_NAME_LEN;
}
Memcpy (name, mServiceName [this - & gt; service_id], MY_PID_NAME_LEN);
If (pid & gt; 0) {//the parent process, the return value is the child of pid
This - & gt; Pid=pid;
return ;
}
Else if (pid==0) {//the child process, the return value of 0
int ret=0;
//the name of the new process of modified
PRCTL (PR_SET_NAME, this - & gt; The name, NULL, NULL, NULL);
//to get his own PID
This - & gt; Pid=getpid ();
//application initialization, here by subclasses override
This - & gt; Server_init (par, len);
//application business cycle, by subclasses override here, should be written in infinite loop
This - & gt; Server_poll ();
Printf (" Warning!!!!!! \n");
The exit (EXIT_FAILURE);
}
The else {
Printf (" Warning!!!!!! \n");
return ;
}
}
The definition of the following for process_sub_1 class
class process_sub_1: public Server {
Private:
Uint32_t void server_init (void * par, len);
Void server_poll (void);
Public:
Uint32_t process_sub_1 (eServer id, void * par, len) : Server (id, par, len) {
}
};
The realization of the process_sub_1 class
# include "process_sub_1. H"
Void process_sub_1: : server_init (void * par, uint32_t len) {
}
Void process_sub_1: : server_poll (void) {
While (1) {
Printf (" process_sub_1 \ n ");
Usleep (1000 * 2000);
}
}
The main main function:
Int main (void) {
Process_sub_1: : install (eSubService1, NULL, 0);
While (1) {
Sleep (1);
}
}
The final test results:
Ubuntu#./bin/my_test
Server
Server
CodePudding user response:
Don't inside the constructor call virtual functions,CodePudding user response:
Please see the explanations in the Efficient c + +CodePudding user response:
Code can't see a virtual keyword, but also rewritten?Even override the virtual function, in the constructor call didn't also effect of polymorphism,
To improve is to use a function independent of the constructor to handle,