Issue with pointers to an abstract class and inheritance
What I'm wanting to do is call a member function of a child class that is not defined in it's abstract parent. If anybody could help me with this, it would be much appreciated.
I have two files:
player.h
player.cpp
player.h contains the definition of an abstract base class "Player", and three external functions to receive pointers to the children. I can not change player.h. It reads as follows:
Code:
class Player {
// A virtual base class, providing the player interface
public:
virtual int bet(unsigned int bankroll,
unsigned int minimum) = 0;
// REQUIRES: bankroll >= minimum
// EFFECTS: returns the player's bet, between minimum and bankroll
// inclusive
virtual bool draw(Card dealer, // Dealer's "up card"
const Hand &player) = 0; // Player's current hand
// EFFECTS: returns true if the player wishes to be dealt another
// card, false otherwise.
virtual void expose(Card c) = 0;
// EFFECTS: allows the player to "see" the newly-exposed card c.
// For example, each card that is dealt "face up" is expose()d.
// Likewise, if the dealer must show his "hole card", it is also
// expose()d. Note: not all cards dealt are expose()d---if the
// player goes over 21 or is dealt a natural 21, the dealer need
// not expose his hole card.
virtual void shuffled() = 0;
// EFFECTS: tells the player that the deck has been re-shuffled.
virtual ~Player() { }
// Note: this is here only to suppress a compiler warning.
// Destructors are not needed for this project.
};
extern Player *get_Simple();
// EFFECTS: returns a pointer to a "simple player", as defined by the
// project specification
along with two other external functions for the other child classes and required #endif.
I then have three derived classes inside of player.cpp, SimplePlayer, CountingPlayer, and Competitor, where the later two are also derived from SimplePlayer. Inside SimplePlayer (and redefined in Competitor) I've added two functions that were not part of Player:
Code:
virtual void lost()
//informs the player of a loss; RME below
{
//intentionally left blank
return;
}
virtual void won()
//informs the player of a win; RME below
{
//intentionally left blank
return;
}
The problem, then, is invoking these two member functions from my main program inside of a different file. In my main, I include player.h. I then create and use a Player object as follows:
Code:
typedef void (Player::*FNSHUFFLE) ();
typedef int (Player::*FNBET)(unsigned int, unsigned int);
typedef bool (Player::*FNDRAW)(Card, const Hand&);
typedef void (Player::*FNEXPOSE)(Card);
******Additional functions, irrelevant********
int main(int argc, char *argv[])
{
FNSHUFFLE shuff = &Player::shuffled;
FNBET bet = &Player::bet;
FNDRAW draw = &Player::draw;
FNEXPOSE expose = &Player::expose;
*******Other declarations, irrelevant*******
Player *player;
if(type == "simple")
{
player = get_Simple();
}
else if(type == "counting")
{
player = get_Counting();
}
else if(type == "competitor")
{
player = get_Competitor();
}
*****SAMPLE MEMBER FUNCTION CALLS BELOW*******
(player->*shuff)();
(player->*expose)(p1);
etc
*******Other parts, irrelevant*******
return 0;
}
This works well, for all of the functions that are native to Player. The Won() and Lost() functions, however, are not part of the Player type and so I am unable to invoke them. Which I'd very much like to be able to do. What possibilities do I have to gain access to the Won() and Lost() functions?