converting from Java

C

cs_hart

I have an application in Java that I would like to port to c++ to
integrate with existing c++ app. Is anyone aware of any tools to help?

I found microsft has a java->c# convert, but the java app has class
that do multiple subclassing and this doesn't seem to work in C#.
Is there any to port C# to c++?
thanks...charlie
 
A

Alessandro Angeli [MVP::DigitalMedia]

I have an application in Java that I would like to port
to c++ to integrate with existing c++ app. Is anyone
aware of any tools to help?

Do you mean managed C++? You can not port Java to native C++
because most Java features require a VM to be supported.
Even if you mean mC++, there is no way to automatically port
Java to C++ AFAIK because it is far from easy to express the
higher level constructs of the Java language and API in C++.

What kind of integration do you need? Maybe you can use JNI
with standard Java or the .NET InterOp services if you port
Java to J# (which is a breeze as long as your Java code
conforms to Java 1.1).
I found microsft has a java->c# convert, but the java app
has class that do multiple subclassing and this doesn't
seem to work in C#.

C# is a superset of the Java language (but for some of the
features introduced in Java5), so you can always port Java
source code to C# (this is not always true for Java API
calls).
Is there any to port C# to c++?

Not that I'm aware of. Even if everything that can be
expressed in C# can also be expressed in C++, some higher
level constructs can not be preserved, just like with Java.
 
I

Ioannis Vranos

Alessandro said:
Do you mean managed C++? You can not port Java to native C++
because most Java features require a VM to be supported.
Even if you mean mC++, there is no way to automatically port
Java to C++ AFAIK because it is far from easy to express the
higher level constructs of the Java language and API in C++.


I do not know C# and Java, but may you provide an example of a Java
construct that cannot be expressed in C++?
 
T

Tom Widmer

Ioannis said:
I do not know C# and Java, but may you provide an example of a Java
construct that cannot be expressed in C++?

Anonymous inner classes can't easily be expressed.

Tom
 
G

Gabest

As I recall Java allows classes to be delcared inside functions, not just
inside other classes. It was a pretty nice feature.
 
A

Alessandro Angeli [MVP::DigitalMedia]

Ioannis said:
Do you mean something like this?


class A
{
class
{
int x;
}y;
};


int main()
{
A a;
}

No, that's just a nested class. He meant something like
this:

public interface I
{
public int f();
};

....
I i = new I() {
public int f() { return 2; }
};
....

In this case, the Java compiler creates an implementing
class that has no name and can not be referenced but by its
instances.

Also, the Java language has a >>> operator and a finally
clause that C++ is missing (but the >>> operator is easy to
implement as an expression).

Anyway, I wrote it is not easy to express some constructs
and not that it is impossible. After all, everything can be
expressed in ASM :)
 
A

Alessandro Angeli [MVP::DigitalMedia]

Gabest said:
As I recall Java allows classes to be delcared inside
functions, not just inside other classes. It was a pretty
nice feature.

Hi there, a familiar face :) I think you are referring to
local classes, which are similar to anoymous classes but not
quite the same. Local class are classes declared inside
methods while anonynous class are declared "inline".
 
I

Ioannis Vranos

Gabest said:
As I recall Java allows classes to be delcared inside functions, not just
inside other classes. It was a pretty nice feature.


Do you mean something like this?


class A
{
public:

void somemethod()
{
class
{
public:
int x;
}y;

y.x=1;
}
};


int main()
{
A a;
}


:)
 
J

Jeff F

Gabest said:
As I recall Java allows classes to be delcared inside functions, not
just inside other classes. It was a pretty nice feature.

void function_with_embedded_class()
{
class embedded_class
{
int member;
public:

embedded_class():member(123){}
}

embedded_class an_embedded_class_instance;
}

Works for me, even in vc6.

Jeff
 
I

Ioannis Vranos

Alessandro said:
No, that's just a nested class. He meant something like
this:

public interface I
{
public int f();
};

...
I i = new I() {
public int f() { return 2; }
};


class I
{
public: virtual int f()=0;
};


class J: public I
{
public: int f() { return 2; }
};


int main()
{
I *i= new J;

// Continue using i
}





...

In this case, the Java compiler creates an implementing
class that has no name and can not be referenced but by its
instances.

Also, the Java language has a >>> operator and a finally
clause that C++ is missing (but the >>> operator is easy to
implement as an expression).


finally is available in many platforms as an extension, as also
"resource acquisition is initialisation technique" which is part of ISO C++.


Anyway, I wrote it is not easy to express some constructs
and not that it is impossible. After all, everything can be
expressed in ASM :)


Well I do not think the above class usage can be termed as "asm". :)
 
G

Gabest

Do you mean something like this?

Yes, that's it. It was a long time ago I tried java, but remembered there
was a tricky class declaration that caught my attention then :)
 
I

Ioannis Vranos

Ioannis said:
class I
{
public: virtual int f()=0;
};


class J: public I
{
public: int f() { return 2; }
};


int main()
{
I *i= new J;

// Continue using i
}


And by using C++/CLI:


public interface class I
{
public: int f();
};



public ref class J: public I
{
public: int f() { return 2; }
};



int main()
{
I ^i= gcnew J;

// ...
}
 
A

Alessandro Angeli [MVP::DigitalMedia]

Ioannis said:
class I
{
public: virtual int f()=0;
};


class J: public I
{
public: int f() { return 2; }
};


int main()
{
I *i= new J;

// Continue using i
}
Well I do not think the above class usage can be termed
as "asm". :)

Of course not, but it is not also equivalent in terms of
code structure since you had to define your derived class J
far from the place you will be instantiang it, while in Java
you can put the derived class definition right where you use
it, without even bothering to give it a name. The only other
language with a similar syntax I know of is ECMAScript.

I admit it doesn't really make a difference to me: I found
anonymous inner classes useful when I program in Java but I
also don't miss them when I program in C++, just like I
don't miss classes when I program in C.

Actually, in C++ I miss inline initialization of dynamic
arrays:

....
int[] x = new int[] { 1, 2, 3, };
....

I could never come up with an equivalent syntax in C++, so
pray tell me if you know of any :)

Just a note: I didn't mean ASM as a bad thing, but the other
way around.
 
I

Ioannis Vranos

Alessandro said:
Of course not, but it is not also equivalent in terms of
code structure since you had to define your derived class J
far from the place you will be instantiang it, while in Java
you can put the derived class definition right where you use
it,


Now we have got into comparisons and these things never end.

But anyway, in C++ you can define a class definition when you first need it:


class I
{
public: virtual int f()=0;
};


int main()
{
class J: public I
{
public: int f() { return 2; }
};

I *i= new J;

// Continue using i
}


without even bothering to give it a name. The only other
language with a similar syntax I know of is ECMAScript.

I admit it doesn't really make a difference to me: I found
anonymous inner classes useful when I program in Java but I
also don't miss them when I program in C++, just like I
don't miss classes when I program in C.

Actually, in C++ I miss inline initialization of dynamic
arrays:

...
int[] x = new int[] { 1, 2, 3, };
...

I could never come up with an equivalent syntax in C++, so
pray tell me if you know of any :)

Just a note: I didn't mean ASM as a bad thing, but the other
way around.


#include <vector>

int main()
{
std::vector<int> somevec;

{
int x[]= { 1, 2, 3 };

somevec.assign(x, x+2);
}
}



But before you become happy, there is important need for a clarification:

What you are referring to as "Java", is essentially the Java language
(syntax) and the Java framework.


So essentially, with Java language you are using Java framework
facilities. If Java framework was open to all languages, the exact same
facilities would be available to all Java framework languages.


C++ on the other hand comes with its compiling portably syntax and
library API, and takes advantage of any framework where it is used by
using the additional facilities provided by that framework.


So for example in .NET (and every other CLI compliant VM) you can simply
do exactly the same as you did:


int main()
{
using namespace System;

array<int> ^x= {1,2,3};

for(long i=0; i<x->Length; ++i)
Console::WriteLine(x);
}


C:\c>cl /clr:safe temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40904
for Microsoft (R) .NET Framework version 2.00.40607.16
Copyright (C) Microsoft Corporation. All rights reserved.

temp.cpp
Microsoft (R) Incremental Linker Version 8.00.40904
Copyright (C) Microsoft Corporation. All rights reserved.

/out:temp.exe
temp.obj

C:\c>


The /clr:safe implies that the produced *binary* is *portable* to all
CLI VMs.



So in this case the framework provides a high level garbage collected
array type with its own methods and properties, and C++ takes advantage
of it.


All high level constructs of the used framework are available to C++,
*including* garbage collection.


The bottom line is, if it exists, C++ uses it.
 
T

Tom Widmer

Ioannis said:
Do you mean something like this?


class A
{
class
{
int x;
}y;
};


int main()
{
A a;
}


No. I mean something like this:

void f(Container<Integer> c, final int i)
{
transform(c, new Transformer<Integer>(){
Integer transform(Integer value)
{
return value + i;
}
});
}

Note:

1. A Transformer subclass (or interface implementation) is defined
inline in the expression, simply by putting a {} after the new-expression.
2. The class definition has access to all of f's "final" local variables
(in this case "i").

This is the single best language feature in Java, IMHO. You can do
simple cases like the above in C++ using boost.lambda, but more complex
cases get out of hand very fast. In effect, Java has a partial
implementation of the functional programming feature "closures", but C++
doesn't have any kind of implementation of it.

Tom
 
T

Tom Widmer

Ioannis said:
Now we have got into comparisons and these things never end.

But anyway, in C++ you can define a class definition when you first need
it:


class I
{
public: virtual int f()=0;
};


int main()
{
class J: public I
{
public: int f() { return 2; }
};

I *i= new J;

// Continue using i

That's nothing like an anonymous inner class. It should be able to
access (const) local variables (of "main" in this case) if it is to be
anything like an anonymous inner class. e.g. in imagined C++

int main(int const argc, char** const argv)
{
I *i= new J(){
int f(){
return argc + argv[0][0];
}
};
}

and have the i pointer be valid even after main has exited (and
therefore the original argc and argv variables are out of scope).
Basically, it doesn't work too well if you haven't got GC.

Best to read up on this. Does C# have anything similar?

Tom
 
I

Ioannis Vranos

Tom said:
That's nothing like an anonymous inner class. It should be able to
access (const) local variables (of "main" in this case) if it is to be
anything like an anonymous inner class. e.g. in imagined C++

int main(int const argc, char** const argv)
{
I *i= new J(){
int f(){
return argc + argv[0][0];
}
};
}

and have the i pointer be valid even after main has exited (and
therefore the original argc and argv variables are out of scope).
Basically, it doesn't work too well if you haven't got GC.


Personally I consider this style tricky and error-prone. And it has not
anything to do with GC, since C++ has the same GC and .NET facilities
that C# has.


I consider the explicit definition of a class derived from the interface
as more safe, while it provides not real "typing" overhead:


In the above you type the definition of f() anyway, and a new class
*gets created* anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top