Use a Cpp function in my Haxe Code

Hi, you can try something like this.

Create Test.h file:

#ifndef __TEST_H
#define __TEST_H

#include <iostream>

class Test {
public:
	Test() {}

	int add(int a, int b) {
		return a + b;
	}


	void print(const char* input){
		std::cout << input << std::endl;
	}

	~Test() {}
};
#endif

Create Main.hx in the same directory:

import cpp.ConstCharStar;
import cpp.Star;

class Main {
	static function main() {
		final testPointer = Test.create();

		trace(testPointer.add(10, 20));

		testPointer.print(ConstCharStar.fromString("Hello World"));
		testPointer.alternatePrint("Hello World 2");

		testPointer.delete();


		final test = Test.make();
		test.print("I'm not a pointer");
	}
}

@:structAccess
@:unreflective
@:include('./Test.h')
@:native('Test')
extern class Test {
	@:native('new Test')
	public static function create():Star<Test>;

	@:native('Test')
	public static function make():Test;

	public function add(a:Int, b:Int):Int;
	public function print(input:ConstCharStar):Void;

	@:native('print')
	public inline function alternatePrint(input:String):Void {
		untyped __cpp__('{0}->print({1}.c_str())', this, input);
	}

	@:native('~Test')
	public function delete():Void;
}

Now, we need to compile and execute our program: haxe -main Main -cpp .\out -cmd out\Main.exe

As a result, you should see:

Main.hx:8: 30
Hello World
Hello World 2
I'm not a pointer

Hope it helps you to start!