I keep on reading that static virtual member functions doesn't make sense because virtual functions will be called based on the type of the class at run time and static functions are not tied to instance. But I am unable to understand this clearly.
1) A virtual table will be created at compile time if we have one or more virtual functions in a class and this virtual table is an array of addresses of those virtual functions. When we create object, a hidden pointer will be created which will be filled with the address of v-table. So when v-table is created at compile time, why compiler can't add addresses of static functions also? If addresses of static functions are also added into v-table, static virtual functions can be possible and we can call those static functions based on the type of class. Is there any specific reason, why this can't be done?
2) Some websites stated that "The static member function can't access non-static data members/functions of a class. The vPTR is non-static data member, hence a static member function can't access vPTR.". But I didn't understand why static member functions need to have access to vPTR, once we reach the function definition through vPTR and v-table.
To be more clear :
class base {
public:
static void test() {
}
};
class derived : public base {
public:
static void test() {
}
};
derived *obj = new derived();
obj->test();
If we add addresses of static member functions to v-table, we can call derived version of test. Please let me know if I am wrong.