How would I use the same instance of a class created in main in another class's function?
For example in main I have the code below. A SignatureBlock
object is created at the beginning and its toString()
function is used shortly after. Later in the code, a Game
object is created and its playGame()
function is called. My problem is that the playGame()
function also needs to use SignatureBlock
's toString()
function.
It seems to me I only have two choices. Either create a new SignatureBlock
object in the Game
class and then use the new object's toString()
function, or pass the SignatureBlock
object created in main to the playGame
function.
I don't really like either of these solution and I was wondering if someone had a better way of doing this.
#include <iostream>
#include "Game.h"
#include "SignatureBlock.h"
int main()
{
SignatureBlock myBlock;
bool done = false;
do
{
std::cout << myBlock.toString();
std::cout << "************\n"
"Tic Tac Toe!\n"
"************\n";
std::cout << "1. Play game!\n"
"2. exit\n"
"\nEnter 1 or 2: ";
std::string option;
std::getline(std::cin,option);
if (option == "1")
{
Game myGame;
myGame.playGame();
}
else if (option == "2")
{
done = true;
}
else
{
std::cout << "Invalid input";
}
}
while (!done);
return 0;
}