#include <iostream>
struct MemA { virtual void tellClass() { std::cout << "I am member of class A" << std::endl; } };
struct MemB : public MemA { void tellClass() { std::cout << "I am member of class B" << std::endl; } };
class A {
MemA *current;
public:
A() : current(new MemA()) {}
void getMemClass() { current->tellClass(); }
~A() { delete current; }
};
class B : public A {
MemB *current;
public:
B() : A(), current(new MemB()) {}
~B() { delete current; }
};
void main() { B().getMemClass(); }
In the above program I have declared the tellClass() function as virtual which means it should decide which function to call at runtime. Despite that it is printing "I am member of class A" even though I am calling getMemClass() from a class B object which has hidden the MemA *current with MemB *current during inheritence.
How do I get this to work properly?