reflaxe.CPP icon indicating copy to clipboard operation
reflaxe.CPP copied to clipboard

function bind was wrong.

Open sonygod opened this issue 2 years ago • 1 comments

class Test {
    
    public function new() {
        
    }

    public function say(){

        trace('hello world');
    }
}
....
var test4=new Test();


var fn=test4.say;

fn();

and get this cpp code

std::shared_ptr<Test> test4 = std::make_shared<Test>();
	std::function<void()> fn = test4->say;//wrong here
	fn();

should be


	std::function<void(void)> fn = std::bind(&Test::say, test4);
	
     // or  which is the better choose?
        auto fn2 = std::mem_fn(&Test::say);
         fn2(test4.get());

sonygod avatar Jun 28 '23 06:06 sonygod

Oooh, nice catch!! I like your solutions too! Wasn't aware of those functions and will definitely use those!!

I was initially thinking to just generate a lambda. But that'd look worse. XD

auto fn2 = []() { test4->say(); };

SomeRanDev avatar Jun 28 '23 06:06 SomeRanDev