I've been reading different questions on this topic, but haven't been able to find one that quite answers what I'm looking for. Here's the pattern of code that I have:
class Base {
public:
virtual void foo(int) const {...}
}
template <class T>
class TmplClass : public Base {
public:
virtual void foo(int i) const { foo(T(i)); }
virtual void foo(T& param) const {
printf("Template::Foo\n");
bar(param);
}
virtual void bar(T&) const {
printf("Template::Bar\n");
}
}
class Derived : public TmplClass<SomeConcreteType> {
public:
void bar(SomeConcreteType&) {
printf("Derived::Bar\n");
}
}
int main() {
Derived d;
Base* b = &d;
b->foo(1);
}
On execution I get:
Template::Foo
Template::Bar
Why doesn't the run-time dispatch on the call to bar work? If I overload foo in Derived then it does call the derived version of foo, why can't it do the dynamic dispatch for bar?
Because I'm working in existing code, I would prefer not to change the basic structure of the classes here. I'm hoping to just find a way to make the call work, or understand why it doesn't. I've tried lots of different things based on reading other questions here, but all to no avail.