// Example program
#include <iostream>
#include <string>
class A;
class B;
void h(A &a) { std::cout << "a"; }
void h(B &b) { std::cout << "b"; }
class A {
public:
virtual void call() { h(*this); }
};
class B: public A {
public:
void call() override { h(*this); }
};
int main()
{
B b = B{};
A &test = b;
h(test); // prints a
test.call(); // prints b
}
I was messing around with code for a bit and noticed that *this always seemed to know which subclass it was, whereas normally we "lose information" when we initialize a subclass as a superclass object.
What exactly is going on behind the scenes to make h(*this) call h(B &b) instead of h(A &a)? What are the two calls in main doing differently such that one prints a, and the other prints b?