473,414 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

converting from Java

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
24 1590
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
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
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
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
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
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
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
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
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
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
>> 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
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
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
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
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
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
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
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
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
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
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: William Krick | last post by:
I've been given the task of converting some existing java code to PHP so we can integrate it into our web based product. The problem I'm facing is that the java code makes heavy use of Vector...
2
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only...
0
by: Elhanan Maayan | last post by:
hi.. so i'm slowly moving the transition to csharp and once again i'm faced with the problem for flipped hebew letters etc... but now the problem may not be so hard to solve as i have seen...
1
by: singlal | last post by:
Hi, We are looking for options of converting CICS screens to a GUI application (.net, Java, etc.) to increase the usability of the screens. We would like to keep the underlying logic and...
9
by: anupamjain | last post by:
Hi, After 2 weeks of search/hit-and-trial I finally thought to revert to the group to find solution to my problem.(something I should have done much earlier) This is the deal : On a JSP...
5
by: ria3 | last post by:
Hi, I have to write a program that will alow the user to specify a number (decimal, binary, or hexadecimal) and a base to convert to (decimal, binary, or hexadecimal) using subroutine methods and...
1
by: patrickdub | last post by:
Hi, I'm wondering what kind of things do I need to look out for when converting my .cmg scripts to Unix scripts. (I want the same inputs and outputs, but I'm now running on a Unix Server. I know...
3
by: notaCons | last post by:
hello im quite newbies in Java Programing so any one can help me. i will be plz.... what am i trying here is try convert The JFrame to JApplet, but when i change the JFrame to JApplet 4 Error come...
1
by: icanhelp33 | last post by:
whats the best way for converting vb.net application to C#
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.