how get a function, from a pointer, to a lambda?

how can i get a function, from a pointer, to a lambda?
1
2
3
test(ClassDerived *clsPointer)
    {
        std::function<void(void)> func =clsPointer->MouseClick;

the function don't have return and no parameters.
error:
"conversion from '<unresolved overloaded function type>' to non-scalar type 'std::function<void()>' requested"
std::function is not itself a lambda, but it can be used to store lambdas and other callable objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <functional>

class ClassDerived
{
public:
	void MouseClick()
	{
		// ...
	}
};

void test1(ClassDerived *clsPointer)
{
	// Stores a member function.
	std::function<void(ClassDerived&)> func = &ClassDerived::MouseClick;
	
	// An object is necessary when calling the function.
	func(*clsPointer);
}

void test2(ClassDerived *clsPointer)
{
	// Stores a lambda that calls a member function on the object.
	std::function<void()> func = [clsPointer]{ clsPointer->MouseClick(); };
	
	// No object needs to be passed in because it's all handled by the lambda.
	// Note that the object pointed to by the pointer that was captured by the 
	// lambda still needs to be valid at this point.
	func();
}
Last edited on
thank you so much for all
i did the test, but don't work for what i need.
thank so much
Topic archived. No new replies allowed.