Thread: Loss of member data

  1. #1
    Registered User
    Join Date
    Mar 2009

    Loss of member data

    Hi all

    Having a problem with a loss of member data when passing the object through to a seperate function. Relevant code fragments are:

    Engine* theEngine = new Engine;
    if (option == 1)
    	Connect4* C4game = new Connect4;
    	theEngine->setStarter(C4game->getPlayers(), C4game->getRows(), C4game->getColumns()); 
    	if (C4game->getPlayers()==1)
    If I use "cout << C4game->getPlayers()" at this stage, it will show the number of players which has been entered (one or two). Next...

    void Engine::RunSinglePlayer (Game* RelevantGame)
    unsigned short CounterLocation;
    	PlayerGo = 1;
    If I were to write "RelevantGame->getPlayers()" at the start of this function, it will automatically show as zero - it's as though the member data was being wiped whilst the parameter is being passed. Between calling the RunSinglePlayer() function and actually arriving there, the data is disappearing.

    Connect4 is a derived class from Game, and they both hold the same member data, so there should be no issue from that perspective (unless I've missed something?).

    Anyone have any ideas why this is happening and how it can be resolved?

    Many thanks! :-)

  2. #2
    Registered User
    Join Date
    Mar 2009
    Following further testing it does seem to be that because the data has now become type "Game" it is trying to use the member data from "Game" (which is blank") instead of the correct data taken from Connect4.

    The idea behind this program is to be able to plug various different types of Game (such as Connect4) into a standard engine. As such I need to be able to transfer the Connect4 (or whatever game is being played) into the engine, and the only way I can think of doing this is to use a parent class that covers all the different sorts of game - however I still need to take all the data and functions from the derived class, not the base class.

    Hope that makes sense! Hope this is possible :S

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    Yeah, put the function in the base class (you do not have to implement them; you can make the base class abstract) and make them virtual. Then the engine would call the proper functions in the derived classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2009
    This tip has helped so much - was using virtual functions but not making them abstract in the base class - so thanks :-) I've actually been told in another post that my whole design is flawed, since the engine should plug into the game and not the other way round :-P would people here agree? (you can tell you're talking to a noob!!)

    Again, thanks for the brilliant tip!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  2. member functions can't see private data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2006, 02:36 PM
  3. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 12:25 PM
  4. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM
  5. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM