How to determine which derived class is referenced when using an array of pointers

I am writing a C ++ program that will download a cube and flip a coin. I need to use inheritance and polymorphism. I have my virtual functions installed correctly. In my base class (aRandomNumberGenerator) a virtual function is created for me. In main (), I need to have an array of 2 base class pointers for my derived classes (aDie and aCoin). How to find out which derived class is indicated in the array when I call the generate () function ??

the code:

int main() { int n; int frequency1 = 0; // count of 1s rolled int frequency2 = 0; // count of 2s rolled int frequency3 = 0; // count of 3s rolled int frequency4 = 0; // count of 4s rolled int frequency5 = 0; // count of 5s rolled int frequency6 = 0; // count of 6s rolled int face; // stores most recently rolled value int numHeads = 0; // count of heads int numTails = 0; // count of tails int side; // stores most recently flipped value cout << "Enter a seed number: "; // promp for user to enter a seed number cin >> n; cout << endl; cout << endl; aRandomNumberGenerator number(n); aDie die(n, n); aCoin coin(n, n); aRandomNumberGenerator *games[2]; // array of 2 base class pointers for(int i=0; i <= 2; i++) games[i]->generate(); // output the seed number entered by the user cout << "the seed entered is: " << die.inputSeed(n) << endl; cout << endl; cout << endl; // summarize results for 600 flips of a coin for(int counter2 = 0; counter2 < 600; counter2++) { side = generate(); // determine flip value 0/1 and increment appropriate counter switch ( side ) { case 0: ++numHeads; break; case 1: ++numTails; break; default: cout << "can only be heads or tails"; } } // summarize results for 600 rolls of a die for(int counter1 = 0; counter1 < 600; counter1++) { face = generate(); // determine roll value 1-6 and increment appropriate counter switch ( face ) { case 1: ++frequency1; break; case 2: ++frequency2; break; case 3: ++frequency3; break; case 4: ++frequency4; break; case 5: ++frequency5; break; case 6: ++frequency6; break; default: cout << "7 doen't exist on a die!"; } } // output results cout << "Heads: " << numHeads << endl; cout << "Tails: " << numTails << endl; cout << "1: " << frequency1 << endl; cout << "2: " << frequency2 << endl; cout << "3: " << frequency3 << endl; cout << "4: " << frequency4 << endl; cout << "5: " << frequency5 << endl; cout << "6: " << frequency6 << endl; cout << endl; cout << endl; system ("pause"); return 0; 
+6
c ++ polymorphism
source share
3 answers

Use dynamic_cast.

 if (Derived1* ptr = dynamic_cast<Derived1*>(basepointer)) { // Do something with ptr } 

However, for a normal method call you do not need to define it. Just call the method in the base class and it will be sent to the correct derived class - which is for inheritance.

Edit: for a regular virtual method, you can just call it on the base pointer and not know or care about what Derived is, and you will get the correct function call.

+8
source share
 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) { // Do something with ptr } 
+3
source share

You can use RTTI (typeid and dynamic_cast) or add a virtual function that identifies the class in the implementation (collapses its own RTTI).

Generally, if you can avoid this, it is considered bad practice to add implementation-dependent code (code that cares about the exact type), except for the implementation itself. By "implementation" I mean not only a specific class of derivatives, but also paired / related classes that are related to this implementation.

Try the switch / else-if commands specific to your exact implementations, in the implementations themselves, through polymorphism.

http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.6

0
source share

All Articles