# How to prevent copying and deleting extern c++ member variables?

**URL:** https://community.haxe.org/t/how-to-prevent-copying-and-deleting-extern-c-member-variables/1308
**Category:** Haxe
**Tags:** haxe-cpp
**Created:** [November 26, 2018, 3:44pm UTC](https://community.haxe.org/t/how-to-prevent-copying-and-deleting-extern-c-member-variables/1308 "2018-11-26T15:44:32Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![pinkboi](https://community.haxe.org/user_avatar/community.haxe.org/pinkboi/32/2416_2.png) [@pinkboi](https://community.haxe.org/u/pinkboi)
#### Post date: [November 26, 2018, 3:44pm UTC](https://community.haxe.org/t/how-to-prevent-copying-and-deleting-extern-c-member-variables/1308/1 "2018-11-26T15:44:32Z")

</div>

If I make an extern class that points to a c++ class like so:

```haxe
@:build(Include.xml('BuildFile'))
@:include("./include/cppclass.h")
@:native("CppClass")
@:structAccess
extern class CppClass {
  @:native("CppClass")
  public static function create():CppClass;
  public function callF(callback:cpp.Callable<(a:Int, b:Float)->Void>):Void;
}

```

It works fine if I use it directly like so:

```haxe
var cppClass = CppClass.create();
cppClass.callF(cpp.Callable.fromStaticFunction(callback));

```

But if I make it a member variable on another, ordinary haxe class, simply calling a method on it, calls the copy constructor:

```haxe
class EncapsulatingClass {
  private var cppClass:CppClass;
  
  private static function callback(a:Int, b:Float) {
    trace(a);
    trace(b);
  }

  public function new() {
    cppClass = CppClass.create();
    cppClass.callF(cpp.Callable.fromStaticFunction(callback));
  }
}

class Main {

  public static function main() {
    trace('hello');
    var c = new EncapsulatingClass();
  }
}

```

Gives me this output:

```haxe
destructor called
Main.hx:25: hello
destructor called
Main.hx:7: 3
Main.hx:8: 2.29999995231628
destructor called

```

I get why the first and second ones happen. It gets instantiated once, then again upon my `create()` (and to my knowledge haxe doesn’t give you a way to specify what goes in the initialization list). I can get around some of the creates by making it a pointer. However, even if I do that, making it a `cpp.Pointer<CppClass>`, calling a method on it requires me to do `cppClass.ref.methodName()`, it still ends up copying it and the local gets destructed _which causes me trouble with the lib I’m currently trying to integrate_. That happens because calling ref dereferences the pointer before returning it.

You can see in the generated c++ where this happens:

```haxe
HXLINE( 12) this->cppClass = CppClass();
HXLINE( 13) CppClass _hx_tmp = this->cppClass;
HXDLIN( 13) _hx_tmp.callF(::cpp::Function< void (int,Float)>(hx::AnyCast(&::EncapsulatingClass_obj::callback )));

```

And again, even if I make the member variable a cpp.Pointer instead, I still get the unnecessary copying and deletion.

You can get the whole working/non-working code at a separate branch I made of a sample extern here:

> <https://github.com/thomasjwebb/hxcpp-buildxml-example/blob/multiple_delete/Main.hx>
