C++ to Java converter

Not sure if such a thing exists, but has anybody heard of such a thing? Does anybody know a simple way of converting C++ classes into Java?



tomcat

I don’t know of any tools that will do it for you automatically. I have a book that has a whole section devoted to it, so if you need specific questions answered I can consult it and get back to you.



But I’ve not heard of a tool that takes a C++ source file and generates a Java source file.

Thx, I thought so but it was worth asking. Basically I need to convert a bunch of C++ classes to java.



My C++ is a bit rusty, so I can use whatever help I can get. Need to know how to convert a C++ class definition to java and find the equivalent of the follwoing in java:



namespace

using namespace

assert

friend

enum

using

virtual

const

<< and >>



and the worst one to consider is the pointers and how to handle them/convert them.



Any ideas where to start?



tomcat

I’ll give the basics from the book, let me know if you want more detail.



namespace is equivalent to packages



assert: It doesn’t have anything regarding that, but you could just add a check and if true throw an exception.



friend: Doesn’t mention anything in the book.



enum: Create a class with public constants. (1.5 introduces enums though)



using: Is equivalent to 1.5’s static imports. pre-1.5 you’ll have to use the fully qualified name, i.e. Math.sqrt(5).



virtual: abstract method or interface.



const: static final



i.e. const MY_VAL = 2;

public static final int MY_VAL=2;



pointers:



a. Pointer Parameters: Most times a simple removal of the pointer operator because Java passes all objects by reference.



b. Operators on arrays: substitute with proper array-indexing statements.



Hope that helps somewhat. The book is older so a few things are missing, but it might get you through.

Lemme see…

"tomcat" wrote:
namespace

By using a package definition.
"tomcat" wrote:
using namespace

Well, basically the same thing.
"tomcat" wrote:
assert

Exists from JDK 1.4 on under the same name. I personally find assertions rather useless. If you can write the code to check for a condition, you can write the correct error handler, too.
"tomcat" wrote:
friend

Does not exist. There is a quite complicated and rather clumsy approach by passing instances of private class definitions, but you do not want to do that :)
"tomcat" wrote:
enum

Not there. From 1.5 onwards it will be there. Right now, you define public static final ints. No type safety though.
"tomcat" wrote:
using

This I don't know.
"tomcat" wrote:
virtual

No need to do that - all method definitions are virtual under java.
"tomcat" wrote:
const

final, mostly at least. It is not a correct const for all purposes.
"tomcat" wrote:
<< and >>

Does not exist.
"tomcat" wrote:
and the worst one to consider is the pointers and how to handle them/convert them.

There you are on your own. You normally need to analyse your C++ Code to find out, what it does. Mostly you will have problems in the "by reference/by value" area of parameter passing. That's the reason why I believe there is no easy ready-to-use solution.

Same is true for memory management. One could easily use malloc/calloc in a C++ program and then you really have to analyse what it means.

May be lots of work to do such a conversion for a big project. And no cook book attempts will solve all problems.

Guys

Thx for the help. I will have a go and see how far I get. Might be useful for me to write a simple C++ program and convert this first. Then on to the bigger program.

tomcat

"tomcat" wrote:
virtual
Use abstact if you want pure virtual functions
"tomcat" wrote:
and the worst one to consider is the pointers and how to handle them/convert them.
All objecs are pointers(including strings) in java exept primitive types(i.e. int, bool,float)