# Passing Julia function as an parameter to a wrapped C++ class

**URL:** <https://discourse.julialang.org/t/passing-julia-function-as-an-parameter-to-a-wrapped-c-class/26775>\
**Category:** New to Julia\
**Tags:** embedding, cxxwrap\
**Created:** [July 25, 2019, 3:40am UTC](https://discourse.julialang.org/t/passing-julia-function-as-an-parameter-to-a-wrapped-c-class/26775 "2019-07-25T03:40:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DuyTran62529](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@DuyTran62529](https://discourse.julialang.org/u/DuyTran62529)\
**Post date:** [July 25, 2019, 3:40am UTC](https://discourse.julialang.org/t/passing-julia-function-as-an-parameter-to-a-wrapped-c-class/26775/1 "2019-07-25T03:40:50Z")

</div>

Hello, I’m trying to pass a Julia function as a callback function to a C++ class wrapped by CxxWrap. I’m not entirely sure how to go about it but here is what I have so far:

The C++ side:

```julia
#include<iostream>
#include "jlcxx/jlcxx.hpp"
#include <julia.h>

class derivedforJulia{
	public:
		//jl_init();

		jl_value_t *jlPtr;

		derivedforJulia(){
			jlPtr = NULL;
		}

		void setJlFnPtr(jl_value_t *x){
			jlPtr = x;
		}

		void reportName(){
			typedef void (*CppPtr)();
			CppPtr rePtr = reinterpret_cast<CppPtr>(jlPtr);
			rePtr();
		}

		~derivedforJulia(){}	

		//jl_atexit_hook(0);
};

JLCXX_MODULE define_julia_module(jlcxx::Module& cbmod)
{
  cbmod.add_type<derivedforJulia>("derivedforJulia")
  .constructor<>()
  .method("setJlFnPtr", &derivedforJulia::setJlFnPtr)
  .method("reportName", &derivedforJulia::reportName);
}

```

The Julia side:

```julia
module CppClass
  using CxxWrap
  @wrapmodule(joinpath("/home/hoangvu/Desktop/Duy/ClassTest/lib","libclasstestlib"))

  function __init__ ()
    @initcxx
  end
end

jlClass = CppClass.derivedforJulia()

jlFn() = println("hello")

jlPtr = @cfunction(jlFn, Cvoid, ())

CppClass.setJlFnPtr(jlClass,jlPtr)

CppClass.reportName(jlClass)

```

The C++ code compiled not problem so I think the problem is on the Julia side. This is the error I got:

```julia
ERROR: LoadError: ReadOnlyMemoryError()
Stacktrace:
 [1] reportName(::Main.CppClass.derivedforJuliaAllocated) at ./none:0
 [2] top-level scope at none:0
 [3] include at ./boot.jl:326 [inlined]
 [4] include_relative(::Module, ::String) at ./loading.jl:1038
 [5] include(::Module, ::String) at ./sysimg.jl:29
 [6] exec_options(::Base.JLOptions) at ./client.jl:267
 [7] _start() at ./client.jl:436
in expression starting at /home/hoangvu/Desktop/Duy/ClassTest/ClassTestJulia.jl:18

```

Does anyone knows how to go about doing this?

Thank you.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [July 25, 2019, 8:26am UTC](https://discourse.julialang.org/t/passing-julia-function-as-an-parameter-to-a-wrapped-c-class/26775/2 "2019-07-25T08:26:23Z")

</div>

I have never used CxxWrap but if I have to guess, you should probably do something like

```julia
		void setJlFnPtr(jl_value_t *x){
			jlPtr = jl_unbox_voidpointer(x);
		}

```

At any rate a good start would be to print the value returned by `@cfunction` on the Julia side and the value of the pointer used in `reportName`.
