Reputation: 209
I need to translate some c++ into java, but I have I few issues.
How to I have to deal with pointers when they are declared as arguments in the Method?
static void test( double *output){}
Also what is and how can I replace struct?
struct test { int t;
int arg;
float *pva;
double *array;
}
And then in the code they use:
double test(struct test *test)
{}
Oh and a last one.. this is also inside struct test, what means :
test->arg
Upvotes: 0
Views: 691
Reputation: 5173
You do not have pointers in Java, only references and only for objects (not for primitive types). References are like pointers, but you use the '.' notation instead of '*' or '->'. Also, you do not need to delete objects in Java, you just stop using them and eventually the garbage collector with destroy them.
Answering your points above, from bottom to top: If test is a pointer to a struct or class in C++, and arg is a member variable of test, then
test->arg
is used to access the member through the pointer. This would map to
test.arg
in Java, if arg is a public member variable of the test object.
I would translate the following:
struct test
{
int t;
int arg;
float *pva;
double *array;
}
...
double test(struct test *test)
{}
to
public class Test
{
public int t;
public int arg;
float [] pva;
double [] array;
}
...
public static double test(Test test)
{}
For the first case, i.e. the function
static void test( double *output){}
you cannot pass a pointer to double and modify the double in Java. You have to return a double. If you need the double also as an input parameter, you specify it as a normal parameter that is passed by value:
static double test(double output)
{}
I hope this helps.
Upvotes: 1
Reputation: 258568
static void test( double *output){}
This needs more context, it can mean a pointer to a double or an array of doubles.
struct test { int t;
int arg;
float *pva;
double *array;
}
A struct
is a class
with default public
access level. You can replace it with a class with public
members.
test->arg
This accesses the arg
member in test
.
Upvotes: 1
Reputation: 69988
(1)
static void test( double *output){}
Here double*
can be replaced with a Double[]
(assume that you do a new Double[]
) and the method can be put inside a class
.
class testMethod {
public static void test (Double []output) { }
}
(2) how can I replace
struct
? It can be replaced with aclass
.
class test {
public int t;
public int arg;
public Float []pva;
public Double []array;
}
(3)
double test(struct test *test) {}
It can be,
double test (test t) {}
(4)
test->arg
Java doesn't have pointers (though it's implemented with reference); so above statement will be test.arg
Upvotes: 0
Reputation: 533492
In the simple cases you can replace float *
and double *
with float []
and double []
However C++ allows you to do all sorts of unpleasant things which are difficult to translate into Java because it is not allowed in Java.
You can replace a struct
with a class
If you want to understand basic C++ syntax, I suggest you read a guide on how to program in C++.
Upvotes: 0