I'm confused... If "main" residents in a header file, which source file will be our "main.cpp"?
I'm confused... If "main" residents in a header file, which source file will be our "main.cpp"?
Devoted my life to programming...
As it happens, this is probably a violation of the standard and certainly a violation of good taste.Rather, 'main' resides in a header file where the class is defined.
Imagine passing your source along and the poor newbie who gets your code really does try reusing the monstrous thing only to find dozens of "multiple definition" errors all of them related to `main'.
Have fun with that $@*&!% newbie!
Thank you; that is exactly what I was talking about.For example, you might be several function calls deep into the program when you realize that the user has supplied some malformed input. You can simply have defined a 'usage' function that reports the error and exits gracefully.
If at all possible, you should design your program such that processing happens after validation so that this sort of event can not happen.
If that is not possible, you should design your program to allow the possibility of recovery.
If that is not possible, such a failure means dying anyway so `atexit' and `exit' are perfectly acceptable.
If necessary, you can even wrap the `exit' call to issue a log in order to make it a more graceful death.
Soma
Last edited by phantomotap; 09-19-2011 at 09:14 PM.
The syntax basically looks like so:
Code:class app : program<app> { void main() { print_args(); } void print_args() { if(argv.empty()) usage(); copy(argv.begin(), argv.end(), ostream_iterator<string>(cout, "\n")); } void usage() { cout << "Usage: " << argv0 << " <input_files>" << endl; exit(); } } start();
It is a last resort because it is placed at the outer most boundary to catch any exception that falls through the cracks of other exception handlers, so to speak. It is hardly unusual for a global main function to have such a last resort exception handler, so the fact that you write a class has nothing to do with it.Originally Posted by gardhr
Presumably the one that includes that header file. I suggest that you do not adopt gardhr's approach, at least not for now.Originally Posted by GReaper
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
O_oCode:class app : program<app> { void main() { print_args(); } void print_args() { if(argv.empty()) usage(); copy(argv.begin(), argv.end(), ostream_iterator<string>(cout, "\n")); } void usage() { cout << "Usage: " << argv0 << " <input_files>" << endl; exit(); } } start();
Yog Sothoth!Code:start();
A lower-case, function-like macro used outside of a function definition all in order to fake the elimination of `main'.
You are doomed to blow your leg off, the legs of your partners, and those of the mail man.
Soma
Technically, yes. Effectively, no.
The standard does not dictate and your personal opinions about what is or is not tasteful has no bearing on how I decide to write my own programs.
Albert Einstein once said "Everything should be made as simple as possible, but not simpler". I think this is a much better design philosephy than the sort of ad-hoc principles that you seem to follow.
There is no last half of that argument. Technically yes. Period. See also:The standard does dictate that you have a main function. So regardless of how you choose to pretend it doesn't exist, it really does. So keep your falsehoods out of this thread.
Quzah.
Hope is the first step on the road to disappointment.
No, it simply provides a direct pathway to egress. Nothing more. Other execeptions are caught elsewhere (or not at all, if you wish), so the behavior compared to a "typical" program is identical.
Why? Because it "defies the norm"? How absurd!
Sorry, that was a typo. No parenthesis should appear after the program instance 'start'.
Your whole argument centers around the wording I chose, to wit "I use a class instead of defining main when I write a program". But I never said that main is not invoked in my programs, nor any such nonsense that it doesn't exist. The implication was simply that I don't use it directly. Is that really so difficult to understand?
I'd prefer the ol' *defining "main" in "main.cpp"* way, thank you. After all, I do a little trick with headers and sources in order when debugging different object files are outputed, while when releasing all is compiled onto a single object file, letting the compiler inline the functions. If I didn't have a "main.cpp" file, my trick would be dead!
Devoted my life to programming...
Of course it does, because you were wrong:There is no "depends". There is no "instead of".You're wrong on all accounts, so it doesn't matter how you word it.
1. You do define main.
2. You do use it directly, because it is always the entry point of your program, and it is always used.
3. It is not part of a class (which is what my post said).
You were wrong, I wasn't. Period.
edit - Furthermore, as to why I wouldn't suggest "your method":Having that be my entire main program in my entire main.cpp isn't any worse than whatever method you're coming up with. At least mine is clear. Putting main off in a header is pointless.Code:#include"myclass.h" int main( void ) { MyClass Main(); return 0; }
edit edit - Also: Go read my first post and your reply. You are wrong. I said 'main is not part of a class in C++'. There is no "depends" in reply to that.
Quzah.
Last edited by quzah; 09-19-2011 at 10:09 PM.
Hope is the first step on the road to disappointment.
You are proposing that your unusual and unnecessary method of doing things be adopted? How absurd!Originally Posted by gardhr
Sure, it may be more akin to what ForeverAndOne is used to in Java, and maybe GReaper likewise finds it familiar, but when in Rome, do as the Romans do... at least until you become a Roman citizen and can get away with acting like you are Greek (with all due respect to GReaper ).
Actually, your program does nothing much. The global main function only contains a return statement, and before that, a declaration of a function named Main that takes no arguments and returns a MyClassOriginally Posted by quzah
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)