Is there any way to call base class method from virtual function as derived class, not as base one? Example code:
class A
{
public:
virtual void a() = 0;
void print() { std::cerr << typeid(decltype(*this)).name(); };
};
class B : public A
{
public:
virtual void a() { print(); }
};
int main()
{
B b;
b.a(); //prints 1A, I want it to print 1B, is it even possible?
}
Just drop the decltype:
void print() { std::cerr << typeid(*this).name(); };
this always points to an instance of the class whose member function its in. this inside A is always an A*. So typeid(decltype(*this)) always gives you A.
On the other hand, typeid(*this) will lookup runtime type information, which will determine that this is really a B (because A is a polymorphic type).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With