multithreading - C++ method pointer to overriden method -


i finished bit of program hoping work way expected to, , turns out, did! here situation:

i have handler class base class:

class handler { public:     handler();     virtual ~handler();     virtual void handle(socket socket);  protected:     virtual void dowork(socket socket);   private:     std::thread * handlerthread;     static void processdata(socket socket); }; 

i have printhandler subclass of handler:

class printhandler : public handler { public:     printhandler();     ~printhandler();  protected:     virtual void dowork(socket socket); }; 

here definitions of dowork in base class handler , subclass printhandler:

void handler::dowork(socket socket) {     std::cerr << "if see this, didn't override dowork function" << std::endl; }  void printhandler::dowork(socket socket) {     std::cerr << "this in base class" << std::endl; } 

when create pointer printhandler , cast pointer handler , call handle method on object via method pointer:

void handler::handle(socket socket) {     handlerthread = new std::thread(&handler::dowork, this, socket); } 

the doworkexecutes subclass method overrode base class method. when override method in manner did, replace base class method in memory? there anyway call original base class dowork method? calling method pointer change method gets called?

in this thread gives means sub class. how call original base class method base class , actual structure of object in memory?

i understand lot answer, if maybe provide knowledge on questions in first paragraph , suggest reading questions in second, think appropriate.

thank you!

when override method in manner did, replace base class method in memory?

no, both functions still exist. overriding affects version chosen when called on particular object; base version still needed objects of type, or other derived classes don't override it.

is there anyway call original base class dowork method?

yes, can call non-virtually:

handler::dowork(); 

does calling method pointer change method gets called?

no, virtual dispatch chooses same override whether call function directly, or via pointer-to-member-funcion. depends on dynamic type of object it's called on.

what actual structure of object in memory?

that's implementation. typically, object contains pointer (known vptr or virtual pointer) class-specific metadata, contains table of pointers virtual functions (known vtable). when (virtually) call virtual function, looks in table find function call. pointer virtual function specify table entry use, rather function call, virtual dispatch still works on type overrides function.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -