Convert C++ code into JAVA
I have some problem with my JAVA program. After a lot of search on internet I found the solution but unfortunately it was in C++. I wanted to convert it into JAVA. Is it possible? Can I convert this line of code from C++ to Java:
Code:
typedef int (*td_fn_LoadingProgress ) (int , void *) ;
Re: Convert C++ code into JAVA
typedef + pointer of function (if my memory does not deceive me)? It will not be possible directly. In the worst case we should cope with the creation of a new type but it will also change the code around:
Code:
public interface FuncCpp {
public int td_fn_LoadingProgress(int a, Object b);
}
or a class like:
Code:
public class FuncCpp {
public int td_fn_LoadingProgress(int a, Object b) {
return 0;
}
}
Obviously with that closures would be simpler
Re: Convert C++ code into JAVA
Then how do I do in the case of pointer to array?
Code:
DcmData ** p_dcmdata;
(*p_dcmdata)->DeleteAllElts();
as DcmData is a class and a method DeleteAllElts is included in this class.
in java at the moment there are no pointers how I'm doing? I already tried
Code:
DcmData [ ] p_dcmdata;
but it is not working ?
Re: Convert C++ code into JAVA
I had trouble understanding the question... With the Reflection API, you could perhaps get out of this situation. You use the object Method (which is a "kind of pointer" and point to a true method of a class). Then use invoke to realize the call.
Code:
public class DcmData
{
public void DeleteAllElts(int a, Object b)
{
System.err.println("Erase the elements ?! "+a+" - "+b);
}
}
Code:
public class main
{
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
DcmData p_dcmdata = new DcmData();
p_dcmdata.DeleteAllElts(0, "foo");
DcmData ptr_dcmdata = p_dcmdata;
Method method_pseudo_ptr = p_dcmdata.getClass().getMethod("DeleteAllElts", int.class, Object.class);
method_pseudo_ptr.invoke(p_dcmdata, 0, "FOO");
}
}
Re: Convert C++ code into JAVA
my question is:
I said something in c++ this way:
Code:
DcmData ** p_dcmdata;
the appeal of the object are of course so it is a pointer matrix, in particular lines
Code:
(*p_dcmdata)->DeleteAllElts();
*p_dcmdata = new DcmData();
I try to convert this to java and in java there is no concept of pointer
Re: Convert C++ code into JAVA
This is normal since you said it like a picture ...
-You must call the method on one (or several) element(s) of the table.
But more broadly, between the message and the other to release memory, I feel that's trying to code in C++, but Java.
Basically you used to do "like that" in C++, and you want to do the same thing in Java.
In my opinion it is a big mistake, because despite a relatively close syntax, concepts and notions one set up by the two languages are very different. And what is appropriate for one is not to another ...
-You should not ask the question of how to "translate" the C++ with Java!
You should really detail what you want to reach (what you want to achieve?) And see how you would do in Java.
Re: Convert C++ code into JAVA
PLZ CONVERT THIS C++ CODE INTO JAVA:
/* bINARY tREE cODE*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *left;
struct node *right;
};
typedef struct node *NODEPTR;
NODEPTR getnode(void)
{
NODEPTR p;
p=(NODEPTR)malloc(sizeof(struct node));
return(p);
}
NODEPTR maketree(int x)
{
NODEPTR p;
p=getnode();
p->info=x;
p->left=NULL;
p->right=NULL;
return p;
}
void setleft(NODEPTR p,int x)
{
if(p==NULL)
printf("void insertion\n");
else if(p->left!=NULL)
printf("invalid insertion\n");
else
p->left=maketree(x);
}
void setright(NODEPTR p,int x)
{
if(p==NULL)
printf("void insertion\n");
else if(p->right!=NULL)
printf("invalid insertion\n");
else
p->right=maketree(x);
}
void pretrav(NODEPTR p)
{
if(p!=NULL)
{
printf("%d\n",p->info);
pretrav(p->left);
pretrav(p->right);
}
}
void intrav(NODEPTR p)
{
if(p!=NULL)
{
intrav(p->left);
printf("%d\n",p->info);
intrav(p->right);
}
}
void posttrav(NODEPTR p)
{
if(p!=NULL)
{
posttrav(p->left);
posttrav(p->right);
printf("%d\n",p->info);
}
}
int main()
{
NODEPTR ptree;
NODEPTR p,q;
int number;
printf("Enter the numbers to be inserted");
scanf("%d",&number);
ptree=maketree(number);
while(scanf("%d",&number)!=EOF)
{
p=q=ptree;
while(number!=p->info&&q!=NULL)
{
p=q;
if(number<p->info)
q=p->left;
else
q=p->right;
}
if(number==p->info)
printf("duplicate");
else if(number<p->info)
setleft(p,number);
else
setright(p,number);
}
printf("preorder traversal");
pretrav(ptree);
printf("inorder traversal");
intrav(ptree);
printf("postorder traversal");
posttrav(ptree);
getch();
return 0;
}