Thread: Access to derived functions

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    43

    Access to derived functions

    Hi all

    Got a problem I constantly run into when creating small games as programming practice. I am currently practicing keeping the game engine seperate from the game itself, passing the specific game into the engine so the latter can do its thing.

    The problem I have is that when creating the Engine's functions, because I want it to process any game I throw at it, I have to use the parent class "Game" as the parameter being passed into the engine, instead of the specific game such as "Connect 4" or "Checkers". In so doing I lose all the functionality of the specific game when passing it into the engine.

    Consider the following fragments that should demonstrate my problem:

    Code:
    Connect4 C4Game; // The Game I will pass into the engine. Connect4 is a derivation of the Game class 
    
    Engine::PlaceCounter(Game& theGame) // A function in the engine to place counters in whatever game is passed in
    {
    unsigned short WhereToPlaceCounter;
    cout << "Please choose where to place counter: ";
    cin >> WhereToPlaceCounter;
    theGame.CheckPlacementPossibility();
    //...
    }
    I would like "CheckPlacementPossibility" to check with the specific game we've passed in that it's OK to place the counter where the user has requested (since where we can place a counter on the Connect 4 grid will be different to where we can place it on the Othello grid, for example). However, the complier refuses - instead it calls the CheckPlacementPossibility function straight from the parent class Game, where I have a statement that simply reads "you shouldn't be chaining this far up!". I would like the function to call CheckPlacementPossibility ()according to whichever type of Game I've passed in (such as Connect 4).

    As I've said I seem to run into this problem all the time and working around it tends to be ugly, legnthy, and often compromises what I originally set out to achieve. If anyone knows of a simple way to resolve this it would be greatly appreciated.

    Thanks :-)
    Last edited by Know_Your_Role; 12-11-2009 at 06:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  2. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  3. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. virtual functions and templates
    By ygfperson in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2003, 01:10 PM