Alternative to c++ static virtual methods
- by Jaime Pardos
In C++ is not possible to declare a static virtual function, neither cast a non-static function to a C style function pointer.
Now, I have a plain ol' C SDK that uses function pointers heavily.
I have to fill a structure with several function pointers. I was planning to use an abstract class with a bunch of static pure virtual methods, and redefine them in derived classes and fill the structure with them. It wasn't until then that I realized that static virtual are not allowed in C++.
Is there any good alternative? The best I can think of is defining some pure virtual methods GetFuncA(), GetFuncB(),... and some static members FuncA()/FuncB() in each derived class, which would be returned by the GetFuncX(). Then a function in the abstract class would call those functions to get the pointers and fill the structure.