![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 1
| Issue with pointers to an abstract class and inheritance 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
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;
}
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;
}
|
| bainevii is offline | |
| | #2 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,357
| Quote:
Depending on your exact problem, you may be able to avoid dynamic_cast by implementing say, the Visitor pattern.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #3 |
| Afraid of widths Join Date: Apr 2008 Location: Chicago
Posts: 887
| Are there any Player classes at the same level in the inheritance tree as SimplePlayer? If not, why not just have main work through the SimplePlayer interface? |
| medievalelks is offline | |
![]() |
| Tags |
| abstract, inheritance, pointers |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C++ templated class multiple inheritance from different unit | noobcpp | C++ Programming | 1 | 10-19-2008 12:10 PM |
| deriving classes | l2u | C++ Programming | 12 | 01-15-2007 05:01 PM |
| Inheritance with pointers:Overloading? | katie | C++ Programming | 3 | 04-13-2004 01:26 PM |
| structure vs class | sana | C++ Programming | 13 | 12-02-2002 07:18 AM |
| Exporting Object Hierarchies from a DLL | andy668 | C++ Programming | 0 | 10-20-2001 01:26 PM |