Loss of member data
Having a problem with a loss of member data when passing the object through to a seperate function. Relevant code fragments are:
If I use "cout << C4game->getPlayers()" at this stage, it will show the number of players which has been entered (one or two). Next...
Engine* theEngine = new Engine;
if (option == 1)
Connect4* C4game = new Connect4;
theEngine->setStarter(C4game->getPlayers(), C4game->getRows(), C4game->getColumns());
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.
void Engine::RunSinglePlayer (Game* RelevantGame)
unsigned short CounterLocation;
PlayerGo = 1;
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! :-)
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
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.
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!