I have two classes A (base) and B (deriving from A):
class A { };
class B : public A
{
int data;
public:
int get_data() { return data; }
};
Now I have a function test which takes base class pointer and calls derived class function :
void test(A * ptr)
{
ptr->get_data();
}
But problem is ptr may point to A's object or B's object. If it points to B's object, then OK, but if to A's object, then it is a problem.
Moreover, I don't want to make get_data() virtual because data is not property of A's object.
How can I check if ptr points to B's object? One solution which I can think is dynamic_cast and check it for NULL. Is it the best solution or can I have a better solution ?
testasvoid test(B * ptr). Then it cannot be abused.