class Receiver:public Task { ... private: //override long Process(); private: ... }; long Receiver::Process() { ... } class Task{ ... private: ... virtual long Process()=0; ... }; inline Task * Task::TaskExec(Thread *ThisThread) { ... long l_retval =-10000; ... try{l_retval =Process();}catch(...){} // ? ... } 

I do not understand how the function Receiver :: Process () is called in the project. Searched through (ctrl + shift + f), found only use in Task :: TaskExec (). Is it possible to call the Process () function, which is virtual (= 0)?

  • The Receiver class inherits the TaskExec function from the Task abstract class, and it calls the overridden virtual function for the Receiver class object. I hope the TaskExec function is not a static function? - Vlad from Moscow

1 answer 1

Something like this (see comments). More or less clear or need to explain in more detail?

 class Base { public: virtual void vf() = 0; void func() { // Здесь вызывается vf() КОНКРЕТНОГО класса, с // использованием т.н. позднего связывания vf(); } }; class Derived: public Base { public: // Перекрытая виртуальная функция void vf() { cout << "In Derived\n"; } }; int main() { // Здесь b, будучи указателем на Base, указывает на объект Derived Base * b = new Derived; // Вступает в игру позднее связывание. В вызове func выясняется, // на что именно указывает b, и внутри вызывается vf() этого объекта. b->func(); } 

By the way, a purely virtual function can have a body and be called:

 class Base { public: virtual void vf() = 0 { cout << "In Base\n"; } void func() { vf(); } }; class Derived: public Base { public: virtual void vf() { Base::vf(); cout << "In Derived\n"; } }; int main() { Base * b = new Derived; b->func(); }