J
josh
Hi, I'm converting (for learning purpose) )a program from Java to C++
and I've a doubt:
In Java every argument (variable and reference types) is passed and
returned in functions by-value so if I want to create inside that
function a matrix (i.e. int a[][]) and than passing the reference to it
I can simply
returning that value:
in main()
....
int z[][] = pass(); // here 'z' contain the reference to the array
object created in pass() by 'a'
....
the method
....
public static int[][] pass()
{
int a[][] = {{1,2},{3,4}};
return a;
}
....
instead in C++
in main()
....
int **p = pass();
....
the function
....
int ** pass()
{
static int z[] = {1,2,3, -1};
static int o[] = {4,5,6,7, -1};
static int j[] = {8,9,10,11,12, -1};
static int *p[3];
p[0] = z;
p[1] = o;
p[2] = j;
return p;
}
....
in the C++ if I don't use the static when I go out from that function
in the caller 'p' has not
the correct array values. In C++ the argument can be passed by-value
and by-reference so
when in the function I call 'return p' is passed its address but if I
don't use static that address is
not more available bacause 'p' is not more available....
is that the correct conversion ?
Thanks
and I've a doubt:
In Java every argument (variable and reference types) is passed and
returned in functions by-value so if I want to create inside that
function a matrix (i.e. int a[][]) and than passing the reference to it
I can simply
returning that value:
in main()
....
int z[][] = pass(); // here 'z' contain the reference to the array
object created in pass() by 'a'
....
the method
....
public static int[][] pass()
{
int a[][] = {{1,2},{3,4}};
return a;
}
....
instead in C++
in main()
....
int **p = pass();
....
the function
....
int ** pass()
{
static int z[] = {1,2,3, -1};
static int o[] = {4,5,6,7, -1};
static int j[] = {8,9,10,11,12, -1};
static int *p[3];
p[0] = z;
p[1] = o;
p[2] = j;
return p;
}
....
in the C++ if I don't use the static when I go out from that function
in the caller 'p' has not
the correct array values. In C++ the argument can be passed by-value
and by-reference so
when in the function I call 'return p' is passed its address but if I
don't use static that address is
not more available bacause 'p' is not more available....
is that the correct conversion ?
Thanks