# Is it possible to create a new constructor in JLCXX\_MODULE?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-create-a-new-constructor-in-jlcxx-module/24455>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 22, 2019, 1:47am UTC](https://discourse.julialang.org/t/is-it-possible-to-create-a-new-constructor-in-jlcxx-module/24455 "2019-05-22T01:47:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alligatorme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alligatorme/32/6962_2.png) [@alligatorme](https://discourse.julialang.org/u/alligatorme)\
**Post date:** [May 22, 2019, 1:47am UTC](https://discourse.julialang.org/t/is-it-possible-to-create-a-new-constructor-in-jlcxx-module/24455/1 "2019-05-22T01:47:51Z")

</div>

```julia
JLCXX_MODULE define_julia_module(jlcxx::Module& types)
{
   types.add_type<World>("World")
    .constructor<const std::string&>()
    .constructor<int_t>(false) // no finalizer
    .method("set", &World::set)
    .method("greet", &World::greet)
    .method("greet_lambda", [](const World& w) { return w.greet(); } );
}

```

As shown above, we can create a new member function “greet\_lambda” that is not defined in C++ class. Is it possibe to create a new constructor? If yes, what is its syntax, may I have a simple demo? Thanks

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [May 24, 2019, 2:16pm UTC](https://discourse.julialang.org/t/is-it-possible-to-create-a-new-constructor-in-jlcxx-module/24455/2 "2019-05-24T14:16:47Z")

</div>

Sorry for the late reply, I don’t follow the forum as closely as I’d like lately.

The preferred way to do this is to add the constructor on the Julia side, using existing C++ constructors. If you absolutely need to do it in C++, you can add a free function with the name of the type and return the value using `create`:

```nohighlight
JLCXX_MODULE define_julia_module(jlcxx::Module& types)
{
   // ...

  types.method("World", [] (double d) { return jlcxx::create<World>("constructed from double"); });

}

```
