COMMUNITY

[hxcpp] question about create new class instance in cpp

package;

import cpp.link.StaticRegexp;
import cpp.link.StaticStd;
import cpp.link.StaticZlib;


class Empty 
{
	
	public var word:String;
	
	public function new (){
		
		
		word = "hello,welcome to haxe";
		
	}
	@:keep
	public function say():Void{
		trace("call haxe function say" + word);
	}
	
 static function main() {
        trace('hello empty library');
		
	
    }

	@:keep
    static function test() {
        trace('hello test function');
    }
	
	@:keep
	static function crateNew():Empty{
		
		return new Empty();
	}
	
}

test.cpp


Empty_obj::test();//ok,


	Empty_obj *ab = new Empty_obj();//is this right?


	ab->say();//error 2001?why?

sorry

libEmpty-debug.lib instead of libEmpty.lib to link

When you’re not sure on how to use the c+ generated code do it in haxe and look at how it’s done.

It seems you need to do hx::ObjectPtr<Empty_obj> ab = Empty_obj::__alloc(HX_CTX), or at least if you use new you need to call ab->__construct() to call the haxe constructor and init the object.