Considering a class "A", its subclass "B" and a function "f". The function "f" calls a method "p" of its single parameter "A* obj", which is a pointer to "A" objects. Now, after creating an object "b" of the class "B" and passing its pointer to "f", "A" gets referenced instead of "B", even if "b" is a "B" object.
#include <iostream>
class A {
public:
void p() {
std::cout << "boring...";
}
};
class B : public A {
public:
void p() {
std::cout << "woo!";
}
};
void f(A * obj) {
obj->p();
}
int main(int argc, char **argv) {
B b;
f(&b);
return 0;
}
Output:
boring...
Why does this happen?