In the following code
#include <iostream>
using namespace std;
class A {
};
class B : public A {
};
class C {
public:
void foo(const A& a) { cout << "A";}
void foo(const B& b) { cout << "B";}
};
int main() {
C c;
const A& o = B();
c.foo(o);
}
I'd like the result to be "B", instead it is "A".
Is there any way that I could call void foo(const B& b)
without resorting to dynamic_cast
?
EDIT: What I want to achieve is for the different functions to obtain different resources from C (i.e some member variables). I could do use virtual functions on A and B and do something like this
void foo(const A& o) {
o.bar(this)
}
and then A or B would gather the resources from C, but I do not want for A or B to know about C.