How to check if C++ abstract method is defined at runtime
class ABase{
public:
virtual void do1() = 0;
};
class BBase: public ABase{
public:
virtual void do1(){}
};
class CBase: public ABase{
public:
};
ABase * base = rand() % 2 ? new BBase() : new CBase();
if(&(base->do1) != 0)
base->do1();
This gives error.
Thanks, Max
As you can't instantiate an abstract class, any class you encounter at runtime will not have any pure virtual methods (unless you're in a constructor or destructor at the time), they'll all have been overriden with a non-pure overrider. There is nothing to check.
An abstract method must be implemented in order for the class to be instantiated. There's no such thing as checking whether a method is implemented, the compiler will do this for you. In this case, you can't have a CBase object because it has abstract methods.
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