简体   繁体   中英

Why Swig generate array for reference of variable in Java.?

I am doing sample Java with C++ application using Swig . I want to pass two values into C++ from Java and get back the Updated values from C++. I am passing two values as pass by reference into C++ from Java. Swig generated a wrapper method as an array for each reference variable .

What i did:

Test.i

%module test

 %{

extern int test(int& val1,int& val3);

 %}

%include <typemaps.i>

%apply int& OUTPUT { int& val1,int& val3 }

extern int test(int& val1,int& val3);

CPP file

int test(int& val1,int& val2)
{

val1=5;
val2=10;

}

testJNI.java:

public class testJNI {

  public final static native int test(int[] jarg1,int[] jarg2);

}

test.java:

public class test {

  public static int test(int[] val2, int[] val2) {

    return testJNI.test(val1,val2);

The Java and JNI files are array datatype for pass by reference of variable.

Is any other way to generate pass by reference of the variable in Java.?

How to get back update values from C++ into Java not as Array format for each variables.?

Are you are trying to get new values in same int variable? It cannot be done in java as primitives are always passed by value. Swig go around it and pass object that can write into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM