Quote:Original post by Zahlman
Throw an exception if something goes wrong in the constructor. Since that's obviously not supposed to happen, it's a valid use of the exception (assuming you can make the ctor exception-safe). Also, the status is *not* going to be "failed" after the main loop ends - if it's possible for something seriously wrong(TM) to happen during that loop, you could throw an exception from there, too. That gets rid of the need for GetStatus() entirely (and it was buggy: (a) it gets called twice at the beginning and we don't know right now that it doesn't have side effects; (b) the return from main is supposed to be 0, i.e. a 'false' value, if everything went ok. The reasoning on that one is that there is only one way for everything to be ok and multiple ways to fail, so the unique 0 value is used for success and non-zero values for error codes. Also sprach POSIX.)
Also, I'd give the game constructor a chance to look at the command-line arguments - you may want that later :)
#include <vector>#include <string>#include <exception> // or should it be <stdexcept>?#include <iostream>using namespace std;int main(int argc, char *argv[]) { vector<string> arguments(argv, argv + argc); // I think that's right try { // I vote against Hungarian notation and I especially hate the 'C' // prefix for classes. Also, this instance is *not* a global. GameManager mainGame(arguments); while (mainGame.doLoop()); // why complicate the interface more than that? } catch (exception& e) { cout << "OH NOES!!!11 " << e.what() << endl; return -1; // some sort of error. // You could use your own exception class and give it a .errorCode() // function or something. } // 0 is returned implicitly here if no exception happened.}
That would just change my code entirely.
How the heck is one variable 'buggy'? It's true. Then, say something fails. It becomes false. The constructor ends before you know it. You check for falsity. Yes, ehh? You end main(). That's it.
Passing main()'s arguments to the constructor may be needed, but if you think about it realy realy hard, you can always provide some other option to change options since I'm not going to run the program from the command line every single time.
Since when was main() needed to return 0? What main() returns is useful for other programs, but nothing else. And if other programs need my program's return code then I wrote them. All I could do then is check if main was fasle or true. Much, much, easier.