Question

I want to assign a specific struct to a member variable in c++, but my attempt fail. after some tests i recognized that after the assigment the variable dont hold the object passed, but rather implicitly calls the copy constructor and just copies the values to a new object. Isn't it possible to overwrite a object variable in c++?

now the detailed situation:

I don't have much experience in c++, but have to write some c++ code due to the attempt to manually make a jni wrapper for an existing little c library. i am realizing it through proxy classes on the java side, which just hold the adresses of the c++ counterparts and redirect method calls. the library consist of a lot of structs, which i wrote java classes for. the assignment and access of the members is realized through getters and setters on the java side.

//C-lib code

struct A {
   int testVal;
   ...  
}

struct B{
   struct A a;
   ...
}

I got two structs. while the first holds some unimportant code, the second sonsist of a member of the first struct.

//java proxy classes

public class A{
   private long ADRESS;

   public native int getTestVal();
   public native void setTestVal(int testVal);
   ...
}

public class B{
   private long ADRESS;

   public native A getA();
   public native void setA(A a);
   ...
}

the only variable in the java counterparts is the long holding the adress, the access to the inner struct in b is submited through the getter and setter methods. the code word native declares that the method is implemented in native code.

//java test class

public class Test{

   public static void main(String[] args){

      A a = new A();
      B b = new B();
      b.setA(a);
   }
}

I wrote a java test class to check whether the assigment succeeds.

//JNI interface method for setA 

   JNIEXPORT void JNICALL ..._setA
    (JNIEnv *env, jobject java_b, jobject java_a){

        B *b = getBoundObject<B>(env, java_b);
        A *a = getBoundObject<A>(env, java_a);

        a->testVal = 999;
        b->a = *a;
        printf("\nBEFORE ASSIGNMENT %i | %i\n", a->testVal, b->a.testVal);

        b->a.testVal = 200;
        printf("\nAFTER ASSIGMENT  %i | %i\n", a->testVal, b->a.testVal);

}

the jni interface implements the layer between c library and java code. it is written in c++. the testVal variable represents a variable which i just used to check whether the objects are the same. the output of the printf calls look like this:

#C-site: BEFORE ASSIGNMENT 999 | 999

#C-site: AFTER ASSIGMENT 999 | 200

isn't there a way to really assign the object to the variable rather than just copy the values? it is important because the objects are still used on the java side. suggestions?

Was it helpful?

Solution

Seems to me that you need to use pointers. Any time you assign an object, the copy constructor is called to copy everything into the new object. If you use pointers, nothing new should be allocated.

Therefore your struct B should keep a pointer to A rather than the A object. Then your setA() parameter should be setA(A* a).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top