# Pass by reference of map typed struct(CxxWrap.jl)

**URL:** <https://discourse.julialang.org/t/pass-by-reference-of-map-typed-struct-cxxwrap-jl/82545>\
**Category:** General Usage\
**Tags:** cxxwrap\
**Created:** [June 10, 2022, 6:04am UTC](https://discourse.julialang.org/t/pass-by-reference-of-map-typed-struct-cxxwrap-jl/82545 "2022-06-10T06:04:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kemu](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@kemu](https://discourse.julialang.org/u/kemu)\
**Post date:** [June 10, 2022, 6:04am UTC](https://discourse.julialang.org/t/pass-by-reference-of-map-typed-struct-cxxwrap-jl/82545/1 "2022-06-10T06:04:38Z")

</div>

pass by reference of map typed struct(CxxWrap.jl)

Hi,

I’m trying to pass the argument by reference from Julia to C++. This argument is simple struct,  
so I used `map_type` at wrap module difinition.

I am having trouble observing the values of arguments on the Julia side that have been changed on the c++ side.

The following is a minimal example.

What is wrong with it?

* * *

# c++

```c++
struct StructTest{
    int a;
};

JLCXX_MODULE define_module(jlcxx::Module& mod)
{
    mod.map_type<StructTest>("StructTest");

    mod.method("increment", [](StructTest& s){
        s.a += 10;
        std::cout << "increment : s.a = " << s.a << std::endl;
        return s; // The object is copied and returned for testing purposes.
    });
}

```

# julia

```julia
module CxxTest
    struct StructTest
        a::Int32
    end

    @wrapmodule(lib_path, :define_module)

    function __init__ ()
        @initcxx
    end
end

```

# Execution Result

```julia-auto
julia> using CxxTest

julia> s = CxxTest.StructTest(10)
CxxTest.StructTest(10)

julia> CxxTest.increment(s)
increment : s.a = 20 # works correctly in the c++ side
CxxTest.StructTest(20) # The return value is also correct.

julia> s.a # The value have not changed.
10                               

```

---

<div class="post-metadata">

**Author:** ![gdmsl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdmsl/32/2876_2.png) [@gdmsl](https://discourse.julialang.org/u/gdmsl)\
**Post date:** [February 17, 2023, 6:50pm UTC](https://discourse.julialang.org/t/pass-by-reference-of-map-typed-struct-cxxwrap-jl/82545/2 "2023-02-17T18:50:09Z")

</div>

Sorry for resurrecting this post. Did you find a solution? I am having the same problem.
