Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this the correct way to use an array of abstract class (C++)?
    primarykey
    data
    text
    <h1>Ignore the fact that I using raw pointers/arrays.</h1> <p>I am making a card game in C++ that has a <code>Player</code> abstract class that is extended by other classes. I need to make an array of pointers to these derived classes. This works, but is there a better way?</p> <pre><code>class Player{ public: Player(); Player(const Player&amp;); Player &amp; operator=(const Player &amp;); virtual void sort() = 0; virtual Card play(Pile*) = 0; void confirmPlay(); void giveHand(Hand&amp;); void giveCard(Card); bool hasCards(); void won(); void resetWins(); int getWins(); virtual ~Player(); protected: int cardsLeft; Hand hand; int cardPlayed; int wins; private: }; class DumbPlayer : public Player { public: DumbPlayer(); Card play(Pile*); void sort(); virtual ~DumbPlayer(); DumbPlayer &amp; operator=(const DumbPlayer &amp;); DumbPlayer(const DumbPlayer&amp;); protected: private: }; class Game{ public: Game(int, Player**&amp;); void playX(int); void playOne(); virtual ~Game(); protected: private: bool done(); Game &amp; operator=(const Game &amp;); Game(const Game&amp;); Pile * piles; Deck deck; int playerCount; Player ** players; }; //implementation not shown to save space, tell me if you would like to see anything. //I think that all of the other class/function names should be good enough to not show. Game::Game(const int pplayerCount, Player**&amp; pplayers) : piles(new Pile[4]), playerCount(pplayerCount), deck(), players(new Player*[playerCount]){ for(int i = 0; i &lt; 4; i++){ piles[i].setSuit(i); } for(int i = 0; i &lt; playerCount; i++){ players[i] = pplayers[i]; } } void Game::playOne(){ deck.shuffle(); //shuffle deck for(int i = 0; i &lt; 4; i++){ piles[i].reset(); //reset piles } Hand hands[playerCount]; //create hands Hand leftovers; deck.dealAll(playerCount, hands, leftovers); //deal the deck int cardsLeftover = leftovers.getSize(); //there are leftover cards, //52/3 has a remainder for(int playerIdx = 0; playerIdx &lt; playerCount; playerIdx++){ (*players[playerIdx]).giveHand(hands[playerIdx]); //this is what //i am unsure about. (*players[playerIdx]).sort(); } int winner = -1; while(!done()){ for(int playerIdx = 0; playerIdx &lt; playerCount; playerIdx++){ Card play = (*players[playerIdx]).play(piles); if(piles[play.getSuit()].canPut(play)){ (*players[playerIdx]).confirmPlay(); piles[play.getSuit()].put(play); if(!(*players[playerIdx]).hasCards()){ winner = playerIdx; break; } } else { if(cardsLeftover &gt; 0){ (*players[playerIdx]).giveCard(leftovers.popCard(--cardsLeftover)); } } } if(winner != -1){ (*players[winner]).won(); break; } } } </code></pre> <p>I know that it is a ton of code (for this site)... i am unsure about the game constructor/class and the lines including <code>(*players[i]).x()</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload