I am new to C++ programming, i have a got doubt while doing some C++ programs, that is how to achieve dynamic binding for static member function. dynamic binding of normal member functions can be achieved by declaring member functions as virtual but we can't declare static member functions as virtual so please help me. and please see the 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 the above code i have assigned Derived class object to a pointer and calling the static member function print() but it is calling the Base class Function so how can i achieve dynamic bind for static member function.