By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,617 Members | 1,596 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,617 IT Pros & Developers. It's quick & easy.

converting from Java

P: n/a
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

Nov 17 '05 #1
Share this Question
Share on Google+
24 Replies


P: n/a
cs*****@yahoo.com wrote:
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.
--

// Alessandro Angeli
// MVP :: Digital Media
// a dot angeli at psynet dot net
Nov 17 '05 #2

P: n/a
Alessandro Angeli [MVP::DigitalMedia] wrote:

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++?


--
Ioannis Vranos
Nov 17 '05 #3

P: n/a
Ioannis Vranos wrote:
Alessandro Angeli [MVP::DigitalMedia] wrote:

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++?


Anonymous inner classes can't easily be expressed.

Tom
Nov 17 '05 #4

P: n/a
Tom Widmer wrote:
Anonymous inner classes can't easily be expressed.

Do you mean something like this?
class A
{
class
{
int x;
}y;
};
int main()
{
A a;
}


--
Ioannis Vranos
Nov 17 '05 #5

P: n/a
As I recall Java allows classes to be delcared inside functions, not just
inside other classes. It was a pretty nice feature.
Nov 17 '05 #6

P: n/a
Ioannis Vranos wrote:
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 :-)

--

// Alessandro Angeli
// MVP :: Digital Media
// a dot angeli at psynet dot net
Nov 17 '05 #7

P: n/a
Gabest wrote:
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".
--

// Alessandro Angeli
// MVP :: Digital Media
// a dot angeli at psynet dot net

Nov 17 '05 #8

P: n/a
Gabest wrote:
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;
}
:-)


--
Ioannis Vranos
Nov 17 '05 #9

P: n/a
Gabest wrote:
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
Nov 17 '05 #10

P: n/a
Alessandro Angeli [MVP::DigitalMedia] wrote:
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". :-)


--
Ioannis Vranos
Nov 17 '05 #11

P: n/a
>> 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 :)
Nov 17 '05 #12

P: n/a
Ioannis Vranos wrote:
Alessandro Angeli [MVP::DigitalMedia] wrote:
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;
};


I think he meant this:

int main()
{

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

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

Nov 17 '05 #13

P: n/a
Ioannis Vranos wrote:
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;

// ...
}


--
Ioannis Vranos
Nov 17 '05 #14

P: n/a
Ioannis Vranos wrote:
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
}

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

// Alessandro Angeli
// MVP :: Digital Media
// a dot angeli at psynet dot net

Nov 17 '05 #15

P: n/a
Alessandro Angeli [MVP::DigitalMedia] wrote:
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[i]);
}
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.


--
Ioannis Vranos
Nov 17 '05 #16

P: n/a
Ioannis Vranos wrote:
#include <vector>

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

{
int x[]= { 1, 2, 3 };
somevec.assign(x, x+3); }
}



--
Ioannis Vranos
Nov 17 '05 #17

P: n/a
Ioannis Vranos wrote:
Tom Widmer wrote:
Anonymous inner classes can't easily be expressed.


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
Nov 17 '05 #18

P: n/a
Ioannis Vranos wrote:
Alessandro Angeli [MVP::DigitalMedia] wrote:
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


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
Nov 17 '05 #19

P: n/a
Tom Widmer wrote:
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.


--
Ioannis Vranos
Nov 17 '05 #20

P: n/a
Perhaps this was not the best example. There is more use of inner classes
when dealing with event listeners. Here is another example I found quickly
with google at
http://www.iam.ubc.ca/guides/javatut...ssAdapter.html .
In this case it would be a hustle to declare the class somewhere else.

public class AnonymousSpot extends Applet {
private Point clickPoint = null;
private static final int RADIUS = 7;
public void init() {
addMouseListener(
new MouseAdapter() {
public void mousePressed(MouseEvent event) {
clickPoint = event.getPoint();
repaint();
}
}
);
}
/* paint method not shown */
}
Nov 17 '05 #21

P: n/a
Now exactly the same, but you may take a look at this:
http://boost.org/libs/assign/doc/index.html

Tom

Alessandro Angeli [MVP::DigitalMedia] wrote:
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 :-)

Nov 17 '05 #22

P: n/a
Gabest wrote:
Perhaps this was not the best example. There is more use of inner classes
when dealing with event listeners. Here is another example I found quickly
with google at
http://www.iam.ubc.ca/guides/javatut...ssAdapter.html .
In this case it would be a hustle to declare the class somewhere else.

public class AnonymousSpot extends Applet {
private Point clickPoint = null;
private static final int RADIUS = 7;
public void init() {
addMouseListener(
new MouseAdapter() {
public void mousePressed(MouseEvent event) {
clickPoint = event.getPoint();
repaint();
}
}
);
}
/* paint method not shown */
}

Well, the above can be easily implemented in C++ by defining a

AnonymousMouseAdapter: public MouseAdapter inside AnonymousSpot.
The important thing to note about C++, is that is not an "exotic
language", you know the cost of everything, so since a class gets
created, a class definition should be provided explicitly.
Still, imagine if C++ provided as an "exotic" built in std::string,
without any clues of its definition.
And that is the strength of C++, it is *both* high-level and low level,
that is it does not lack any real high level facilities.

In usual applications you can ignore the internals of std::string by
using "selective ignorance" and use it as an "exotic" type.
However C++ aims also to be a powerful language, and power has details.
Also, again, GC is available to C++ when it exists.
Can you do this in Java?


// (Compile time) template
template <class T>
ref class SomeClass1
{
array<T> ^someArray;
};
// (Run time) generic
generic<class T>
ref class SomeClass2
{
array<T> ^someArray;
};

ref class SomeOtherClass {};

int main()
{
// In the GC heap
SomeClass1<SomeOtherClass ^> ^h1= gcnew SomeClass1<SomeOtherClass ^>;

// In the GC heap with stack semantics
// Deterministic destruction at the end of scope
SomeClass2<SomeOtherClass ^> obj1;

SomeClass1<SomeClass2<SomeOtherClass ^> ^>obj2;
SomeClass2<SomeClass1<SomeOtherClass ^> ^> ^h2;

//Deterministic destruction
delete h2;
}
C:\c>cl /clr:safe temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.41013
for Microsoft (R) .NET Framework version 2.00.41013.0
Copyright (C) Microsoft Corporation. All rights reserved.

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

/out:temp.exe
temp.obj

C:\c>

As you see both compile-time templates and run-time generics are
available, can be mixed, and produce pure IL code (/clr:safe) making the
produced executable binary portable to all CLI VMs.


--
Ioannis Vranos
Nov 17 '05 #23

P: n/a
Ioannis Vranos wrote:
Well, the above can be easily implemented in C++ by defining a

AnonymousMouseAdapter: public MouseAdapter inside AnonymousSpot.
The important thing to note about C++, is that is not an "exotic
language", you know the cost of everything, so since a class gets
created, a class definition should be provided explicitly.
Still, imagine if C++ provided as an "exotic" built in std::string,
without any clues of its definition.
And that is the strength of C++, it is *both* high-level and low level,
that is it does not lack any real high level facilities.

In usual applications you can ignore the internals of std::string by
using "selective ignorance" and use it as an "exotic" type.
However C++ aims also to be a powerful language, and power has details.
Also, again, GC is available to C++ when it exists.
Can you do this in Java?


// (Compile time) template
template <class T>
ref class SomeClass1
{
array<T> ^someArray;
};
// (Run time) generic
generic<class T>
ref class SomeClass2
{
array<T> ^someArray;
};

ref class SomeOtherClass {};

int main()
{
// In the GC heap
SomeClass1<SomeOtherClass ^> ^h1= gcnew SomeClass1<SomeOtherClass ^>;

// In the GC heap with stack semantics
// Deterministic destruction at the end of scope
SomeClass2<SomeOtherClass ^> obj1;

SomeClass1<SomeClass2<SomeOtherClass ^> ^>obj2;

SomeClass2<SomeClass1<SomeOtherClass ^> ^> ^h2=
gcnew SomeClass2<SomeClass1<SomeOtherClass ^> ^>;


//Deterministic destruction
delete h2;
}
C:\c>cl /clr:safe temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.41013
for Microsoft (R) .NET Framework version 2.00.41013.0
Copyright (C) Microsoft Corporation. All rights reserved.

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

/out:temp.exe
temp.obj

C:\c>

As you see both compile-time templates and run-time generics are
available, can be mixed, and produce pure IL code (/clr:safe) making the
produced executable binary portable to all CLI VMs.



--
Ioannis Vranos
Nov 17 '05 #24

P: n/a
> 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?


In C# 2.0 there is support for anonymous methods which do provide parameter
and local capture(via a heap allocated container object, if memory serves),
but do not define whole, user accessible types(they do generate classes
behind the scene to contain the method and the parameter object, if memory
serves.)

These methods, when created, are delegate objects which can be assigned to
events or passed into some of the newer, delegate based API's that are being
introduced in the 2.0 framework.

the syntax would be something like...

static void Main(string[] args)
{
MethodInvoker f = delegate() { Console.WriteLine(args[0]); };
}

FWIW, I think this is *close* to the concept of a closure, but I am not sure
if it satisfies all the requirements or not.
Nov 17 '05 #25

This discussion thread is closed

Replies have been disabled for this discussion.