-1

In one homework project I need to use an object, and its initialization controlled by if condition, so it seems a little complicated, thus I want to make the initialization process as a function. But is it possible for me to initialize this object inside a function and then use it outside?

Here's the code snippet:

void playOneGame(Lexicon& dictionary) {
// TODO: implement
setConsoleClearEnabled(true);

// initialize object: if yes, generate it randomly, else by human input
if (getYesOrNo("Do you want to generate a random board?")) {
    // generate boggle randomly by using constructor
    Boggle myBoggle(dictionary, "");
} else {
    string boardText = getLine("Type the 16 letters to appear on the board:"); 
    //boardText = stripText(boardText);
    while (boardText.length() != 16 || containsNonAlpha(boardText)) {
        cout << "That is not a valid 16-letter board string. Try again." << endl;
        boardText = getLine("Type the 16 letters to appear on the board:");
    }  

}
Lake_Lagunita
  • 521
  • 1
  • 4
  • 20
  • 1
    What happened when you tried? – Scott Hunter Jul 29 '15 at 17:22
  • 1
    Sure, either create it locally and return a copy, or create it dynamically and return a smart pointer – KABoissonneault Jul 29 '15 at 17:28
  • Since it's homework: maybe use a pointer to a pointer. – Stefan Jul 29 '15 at 17:29
  • I think I've solved it. – Lake_Lagunita Jul 29 '15 at 17:30
  • I would suggest adding a public method to the `Boggle` class that you can call when needed to (re-)initialize it with either a `Lexicon` or a `string`. Then declare your `myBoggle` variable without specifying any constructor parameters, and call the init method with the desired input: `Boggle myBoggle; ... myBoggle.init(dictionary);` or `myBoggle.init(boardText);`. If you must use a constructor, you can use `myBoggle = Boggle(dictionary);` and `myBoggle = Boggle(boardText);`. Or use `new` to allocate `myBoggle`: `Boggle *myBoggle; ... myBoggle = new Boggle(dictionary); ... delete myBoggle;` – Remy Lebeau Jul 29 '15 at 17:34
  • 1
    @LeonloveKaren Then share you solution. It may be helpful to others. – Piotr Siupa Jul 29 '15 at 17:35

1 Answers1

1
void playOneGame(Lexicon& dictionary) {
// TODO: implement
setConsoleClearEnabled(true);

Boggle myBoggle = setupBoard(dictionary);

}

Boggle setupBoard (Lexicon& dictionary) {
if (getYesOrNo("Do you want to generate a random board?")) {
    Boggle myBoggle(dictionary, "");
    return myBoggle;
} else {
    string boardText = getLine("Type the 16 letters to appear on the board:");
    //boardText = stripText(boardText);
    while (boardText.length() != 16 || containsNonAlpha(boardText)) {
        cout << "That is not a valid 16-letter board string. Try again." << endl;
        boardText = getLine("Type the 16 letters to appear on the board:");
    }
    Boggle myBoggle(dictionary, boardText);
    return  myBoggle;
}

}

Here's the solution

Lake_Lagunita
  • 521
  • 1
  • 4
  • 20
  • You could write simply `return Boggle(dictionary, "");`. I believe that might also help the compiler optimize away the copy using the [return value optimization (RVO)](http://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization). Multiple return points with a named variable are one of the cases where a compiler may fail to elide the copy. – Chris Drew Jul 30 '15 at 10:24