aRandomNumberGenerator *games[2]; // array of 2 base class pointers for(int i=0; i <= 2; i++) games[i]->generate();
First, let it work before we deal with other things.
You create an array that will contain pointers to two aRandomNumberGenerator objects, but you will never create aRandomNumberGenerator objects themselves. games[0] and games[1] contain meaningless meanings. In addition, in the loop you also get access to games[2] , which does not exist.
UPDATE: For some reason, the other responder decided to use a fancy short version of the code that I hate, and would probably confuse any inexperienced C ++ code (which is obviously OP), so let's do it ourselves:
Derived1* ptr = dynamic_cast<Derived1*>(basepointer) if (ptr != null) {
James curran
source share