Thread: Convert Java To C++

  1. #16
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    I'm confused... If "main" residents in a header file, which source file will be our "main.cpp"?
    Devoted my life to programming...

  2. #17
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Rather, 'main' resides in a header file where the class is defined.
    As it happens, this is probably a violation of the standard and certainly a violation of good taste.

    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!

    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.
    Thank you; that is exactly what I was talking about.

    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.

  3. #18
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    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();

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gardhr
    It isn't a really "last resort"
    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.

    Quote Originally Posted by GReaper
    I'm confused... If "main" residents in a header file, which source file will be our "main.cpp"?
    Presumably the one that includes that header file. I suggest that you do not adopt gardhr's approach, at least not for now.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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();
    O_o

    Code:
    start();
    Yog Sothoth!

    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

  6. #21
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by quzah View Post
    Hiding 'main' away in a header file doesn't magically make it not part of your program. So there is no 'depends' answer.

    Quzah.
    Technically, yes. Effectively, no.

    Quote Originally Posted by phantomotap View Post
    As it happens, this is probably a violation of the standard and certainly a violation of good taste.
    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.

    Quote Originally Posted by phantomotap View Post
    Thank you; that is exactly what I was talking about.

    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.
    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.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by gardhr View Post
    Technically, yes. Effectively, no.
    There is no last half of that argument. Technically yes. Period. See also:
    Quote Originally Posted by gardhr View Post
    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.
    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.

  8. #23
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by laserlight View Post
    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.
    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.

    Quote Originally Posted by laserlight View Post
    Presumably the one that includes that header file. I suggest that you do not adopt gardhr's approach, at least not for now.
    Why? Because it "defies the norm"? How absurd!

    Quote Originally Posted by phantomotap View Post
    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.
    Sorry, that was a typo. No parenthesis should appear after the program instance 'start'.

  9. #24
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by quzah View Post
    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.
    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?

  10. #25
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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...

  11. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by gardhr View Post
    Your whole argument centers around the wording I chose
    Of course it does, because you were wrong:
    Quote Originally Posted by gardhr View Post
    , to wit "I use a class instead of defining main when I write a program".
    There is no "depends". There is no "instead of".
    Quote Originally Posted by gardhr View Post
    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?
    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":
    Code:
    #include"myclass.h"
    int main( void )
    {
        MyClass Main();
        return 0;
    }
    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.

    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.

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by gardhr View Post
    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?
    Not defining main means that main is not invoked and if main is never defined the program cannot link. Further if you do this instead of that the logical conclusion is that never happens. So you did trap yourself.

  13. #28
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    CHI!

    Okay... I'm done.

    Soma

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gardhr
    Why? Because it "defies the norm"? How absurd!
    You are proposing that your unusual and unnecessary method of doing things be adopted? How absurd!

    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 ).

    Quote Originally Posted by quzah
    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.
    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 MyClass
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    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 MyClass
    That's why I don't post on the C++ forum usually. My programs don't do anything much.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. java to c.
    By radxxx in forum C Programming
    Replies: 3
    Last Post: 04-11-2009, 08:56 AM
  2. Multiple Java files for one Java project
    By doubleanti in forum Windows Programming
    Replies: 2
    Last Post: 11-22-2004, 02:06 AM
  3. How to use Java with C++
    By Arrow Mk 84 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2003, 04:12 PM
  4. I use Java but...
    By zurdo1119 in forum C Programming
    Replies: 4
    Last Post: 08-04-2002, 02:05 PM
  5. C# vs. JAVA
    By talal*c in forum C# Programming
    Replies: 10
    Last Post: 04-06-2002, 03:45 AM