Static member functions are simply simple functions (like non-member functions) that are inside the class namespace. This means that you can consider them as simple functions, and the following solution should do:
class Interface { public: void (*function) (); }; class Implementation: public Interface { public: Implementation() { function = impl_function; } private: static void impl_function() {
then
Implementation a; Interface* b = &a; b->function(); // will do something...
The problem with this approach is that you will do almost what the compiler does for you, when you use virtual member functions, itβs just better (less code is needed, less error prone, and the pointer to the implementation functions is split). The main difference is that when using virtual
your function would receive the (invisible) this
parameter when called, and you could access member variables.
Thus, I would recommend that you simply not do this and use the usual virtual methods.
source share