It is not very clear for me how and why can I give the function WhatType() arguments of type Derived1 and Derived2, and why the output is:
ob is referencing an object of type 4Base
ob is referencing an object of type 8Derived1
ob is referencing an object of type 8Derived2
#include <iostream>
#include <typeinfo>
using namespace std;
class Base {public: virtual void f () { } };
class Derived1: public Base { };
class Derived2: public Base { };
void WhatType(Base &ob){
cout << "ob is referencing an object of type " << typeid(ob).name() << endl;}
int main() {
Base b;
Derived1 d1;
Derived2 d2;
WhatType(b);
WhatType(d1);
WhatType(d2);
return 0;
}
I think I have seen before this type of "problem" and kind of memorized how it happens, but I would like to have some rigorous explanation.