I have something like this:
#include <iostream>
class X;
class A {
public:
virtual void bar(X &x);
};
class B : public A {
public:
};
class X {
public:
void foo(A &a) { std::cout << "foo A" << std::endl; }
void foo(B &b) { std::cout << "foo B" << std::endl; }
};
void A::bar(X &x) { x.foo(*this); }
int main(int argc, char **argv) {
X x;
B b;
b.bar(x);
return 0;
}
Compile it and execute it, you'll have:
# ./a.out
foo A
#
I believe this is because object is sliced when cast to A. How can I avoid this so I get
foo B
without implementing the method in B or using some weirdness like the Curiously recurring template pattern ?