I'm having a little issue with overriding static methods of base clases, but the whole question is very complicated and too long (generalization of resource management in game engine), so here's a simplified version:
template<class T>
class base
{
static void bar()
{ printf("bar"); }
public:
static void foo()
{ bar(); }
};
class derived : public base<int>
{
static void bar()
{ printf("baz"); }
};
int main() { derived::foo(); }
The code above outputs "bar" in my case, insted I want it to output "baz". How can I go about that? It seems that no matter what I attempt, base::foo() always calls base::bar(). I might have an issue with the design. I've never came across this problem - how can I resolve it?
What you're trying to do is not achievable with simple class inheritance; a method cannot be both
static
andvirtual
.You need a
static
method to be able to call a function without an object (an instance); and you needbar
to bevirtual
so thatbar<int>::foo()
callsderived::bar()
when called from aderived
instance.Those two traits are mutually exclusive. But the Curiously Recursive Template Pattern (CRTP) may be a solution here:
Live example