Convert Java To C++

This is a discussion on Convert Java To C++ within the C++ Programming forums, part of the General Programming Boards category; Hello I wrote this out to count out the letters in a sequence for use in decryption class. but I ...

Depends. Personally, I prefer to use a class in place of 'main'. For one thing, it can make accessing command line parameters and environment variables much simpler by prepackaging them into more user friendly formats. Since your member variables can take the place of globals, you can utilize less verbose function calls without resorting to uncompartmentalized coding. Finally, instead of returning a value, you can simply define an 'exit' function in your 'main' base class that throws an exception to be caught from the wrapped 'main'/'wmain' to do so, making it easy to safely return from anywhere in your 'main' class' functions.

[Edit]Okay, I can't find the quote so I'll guesstimate. The point here is that Java makes you jump through hoops like that imposing a level of "OOP" that is absolutely not needed in C and C++; you don't need to bring those mistakes home, so don't.[/Edit]

Personally, I prefer to use a class in place of 'main'. For one thing, it can make accessing command line parameters and environment variables much simpler by prepackaging them into more user friendly formats.

A standard C or C++ program begins with `main'; you don't "use a class in place of 'main'"; you are simply calling another function.

Since your member variables can take the place of globals, you can utilize less verbose function calls without resorting to uncompartmentalized coding.

Finally, instead of returning a value, you can simply define an 'exit' function in your 'main' base class that throws an exception to be caught from the wrapped 'main'/'wmain' to do so, making it easy to safely return from anywhere in your 'main' class' functions.

O_o

Now you've committed a terrifying assault on program logic while managing to poorly hide what is essentially a `goto' or `setjmp'/`longjmp' and badly implementing a measure of the behavior of the standard `exit' function.

Not at all. Suppose you wanted to use the class in a different context (that is, not as the actual main driver of your program). You could basically just change the name of the base class and reuse the code as-is, without dragging around global declarations that could conflict elsewhere. It's just a cleaner design, really.

Originally Posted by phantomotap

O_o

Now you've committed a terrifying assault on program logic while managing to poorly hide what is essentially a `goto' or `setjmp'/`longjmp' and badly implementing a measure of the behavior of the standard `exit' function.

How so? The stack unwinds safely, so destructors are called appropriately, plus you get to write more lucid code to boot. I don't see anything inherently flawed with that.

Originally Posted by phantomotap

[Edit]Okay, I can't find the quote so I'll guesstimate. The point here is that Java makes you jump through hoops like that imposing a level of "OOP" that is absolutely not needed in C and C++; you don't need to bring those mistakes home, so don't.[/Edit]

Oh, I see. You just hate the idea of using a class instead of main. Got it.

More accurately, you use a class in addition to defining main when you write a program.

Originally Posted by gardhr

Not at all. Suppose you wanted to use the class in a different context (that is, not as the actual main driver of your program). You could basically just change the name of the base class and reuse the code as-is, without dragging around global declarations that could conflict elsewhere. It's just a cleaner design, really.

I am curious as to the feasibility of reusing a "main class" in the first place.

Originally Posted by gardhr

How so? The stack unwinds safely, so destructors are called appropriately, plus you get to write more lucid code to boot. I don't see anything inherently flawed with that.

I agree. However, this has less to do with the class and more to do with having that last resort exception handler in main.

Can you please explain that? I thought that a C++ program HAS to have its "main" defined. Is there another way?!

gardhr's approach is to write a class whose constructor or other, possibly static, member function contains the code that would normally be in the global main function. The global main function is then implemented to use this class, e.g., by calling the static member function or instantiating an object of the class (and possibly calling the designated member function).

This approach is uncommon in C++. I think that it would be more common to write another function that is called from the global main function. This function would contain the code that would normally be in the global main function. The global main function itself would then do things like translate the command line arguments to a preferred format.

More accurately, you use a class in addition to defining main when you write a program.

Rather, 'main' resides in a header file where the class is defined.

Originally Posted by laserlight

I am curious as to the feasibility of reusing a "main class" in the first place.

Well, let's say you have a program that provides some file conversion facility. You have another program that performs some higher level functionality and decide to "drag and drop" it into that codebase. Point it, if you really wanted to do it, you could do so with minimal effort.

Originally Posted by laserlight

I agree. However, this has less to do with the class and more to do with having that last resort exception handler in main.

It isn't a really "last resort", but a practical way to return a value to the operating system without having to engineer the program with all of the "plumbing" needed to safely do so without such a facility. 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. The only safe alternative would be to return an error code or whatnot from each function, which has to be rechecked at multiple points before you get down to the business of shutting the program down.

Originally Posted by GReaper

Can you please explain that? I thought that a C++ program HAS to have its "main" defined. Is there another way?!

See above. Basically, it entails invoking 'main' within the header file, which in turn calls the 'main' function of the wrapper class.