Thread: Issue with pointers to an abstract class and inheritance

  1. #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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-19-2008, 12:10 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Inheritance with pointers:Overloading?
    By katie in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2004, 01:26 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

Tags for this Thread