I have a singleton instance of a class which derives from a base call. So far, so good. Derived
has a static function fun
, while Base
has a member function fun
(don’t ask). Obviously, calling singleton->fun()
in Derived::fun()
create an infinite recursion ending in stack overflow. I managed to avoid that by calling ((Base*)singleton)->fun()
instead:
struct Base {
void fun() { /* ... */ }
};
struct Derived;
static Derived * singleton = nullptr;
struct Derived : Base {
static void fun() {
// stack overflow!
// singleton->fun();
// works!
((Base*)singleton)->fun();
}
};
int main() {
singleton = new Derived();
Derived::fun();
}
Is there a better way to avoid calling the static function defined in Derived
?