C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-30-2009, 11:24 PM   #1
Registered User
 
Join Date: Mar 2009
Posts: 1
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?
bainevii is offline   Reply With Quote
Old 03-30-2009, 11:37 PM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,357
Quote:
Originally Posted by bainevii
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?
You can:
  • Make them virtual functions in the abstract base class.
  • Access the derived class object directly instead of via a pointer or reference to the base class.
  • Use dynamic_cast to case the base class pointer or reference to a pointer or reference to the derived class.
The first option may not fit your design well, the second option may not be feasible, and the third option tends to be an ugly hack that is usually best avoided, especially since it can incur a significant runtime penalty if your class hierarchy is deep.

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   Reply With Quote
Old 03-31-2009, 07:51 AM   #3
Afraid of widths
 
medievalelks's Avatar
 
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   Reply With Quote
Reply

Tags
abstract, inheritance, pointers

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22