c++ - static member functions inheritance -


i new c++ programming, have got doubt while doing c++ programs, how achieve dynamic binding static member function. dynamic binding of normal member functions can achieved declaring member functions virtual can't declare static member functions virtual please me. , please see below example:

#include <iostream> #include <windows.h> using namespace std;  class classa {      protected :      int width, height;      public:      void set(int x, int y)      {        width = x, height = y;      }      static void print()      {        cout << "base class static function" << endl;      }      virtual int area()      {        return 0;       } };  class classb : public classa {      public:    static void print()    {     cout << "derived class static function" << endl;    }    int area()    {     return (width * height);    } };  int main() {   classa *ptr = null;   classb obj;   ptr = &obj ;   ptr->set(10, 20);   cout << ptr->area() << endl;   ptr->print();   return 0; } 

in above code have assigned derived class object pointer , calling static member function print() calling base class function how can achieve dynamic bind static member function.

the dynamic binding want non-static behavior.

dynamic binding binding based on this pointer, , static functions definition don't need or require this pointer.

assuming need function static in other situations (it doesn't need static in example) can wrap static function in non-static function.

class classa {      // (the rest of class unchanged...)       virtual void dynamic_print()      {          classa::print();      } };  class classb : public classa {      // (the rest of class unchanged...)       virtual void dynamic_print()      {          classb::print();      } }; 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -