Consider the following code:
#include <typeinfo>
#include<iostream>
class Parent
{
public:
virtual void foo() const{};
};
class Child_A: public virtual Parent
{
};
void downcast( const Child_A& aa)
{
}
void upcast( const Parent& pp )
{
std::cout<< typeid( pp ).name() << std::endl;
downcast(pp); // line 21: This is the problematic line!
}
int main()
{
Child_A aa;
upcast(aa);
return 0;
}
I get a compiling error:
initialization.cpp:21:14: error: invalid initialization of reference of type 'const Child_A&' from expression of type 'const Parent'
downcast(pp);
But if I compile the code without line 27, typeid( pp ).name()
returns 7Child_A
.
So why do I get this error? How can I fix it? and What is really the type of pp
?