Consider the following code:
class A {
public:
virtual ~A() {}
};
class AA : public A {
};
////////////////////////////////////////
class B {
public:
virtual void f(const A &a) {
// code for A
}
};
class BB : public B {
public:
virtual void f(const AA &a) {
// code for AA
}
};
////////////////////////////////////////
int main() {
A *a = new AA;
B *b = new BB;
b->f(*a);
}
Obviously, the vtables are constructed such that when the above is run, // code for A
is executed. I am looking for a way to be able to execute instead // code for AA
.
The motivation is that this is for a library of code where the end-user will often have to write classes of the form BB, and I would like the process to be as easy as possible (i.e. the user should not have to use RTTI to figure out what derived class of A they are dealing with). Any ideas (and voodoo from any version of the C++ standard) are appreciated.