i'm trying to understand a concept and an error. what's wrong with this?
class A
{
public:
A()
{
std::function<void(int)> testFunc(&A::func);
}
private:
void func(int) {}
}
my question, is it possible to create any sort of object that is able to call a member of a specific instance where std::function acts like a member function pointer, except without the wacky type definition that can't be used as function parameters in inheriting classes. for example:
class A
{
public:
A()
{
index[WM_CREATE] = &A::close;
index[WM_DESTROY] = &A::destroy;
}
protected:
map<UINT msg, void (A::*)(HWND, UINT , WPARAM, LPARAM)> index;
void close(HWND,UINT, WPARAM, LPARAM);
void destroy(HWND, UINT, WPARAM, LPARAM);
};
class B : public A
{
public:
B()
{
index[WM_CREATE] = &B::create; // error because it's not a pointer of type A::*
}
private:
void create(HWND, UINT, WPARAM, LPARAM);
};
I'm thinking i'm on the right track of using std::functions like so:
class A
{
public: // Gigantic stl error with these two
A() // |
{ // V
index[WM_CREATE] = std::function<void(HWND, UINT, WPARAM, LPARAM>(&A::close);
index[WM_DESTROY] = std::function<void(HWND, UINT, WPARAM, LPARAM>(&A::destroy);
}
protected:
map<UINT msg, std::function<void(HWND, UINT, WPARAM, LPARAM)> > index;
void close(HWND,UINT, WPARAM, LPARAM);
void destroy(HWND, UINT, WPARAM, LPARAM);
};
class B : public A
{
public: // and this one
B() // |
{ // V
index[WM_CREATE] = std::function<void(HWND, UINT, WPARAM, LPARAM)>(&B::create);
}
private:
void create(HWND, UINT, WPARAM, LPARAM);
};
if someone could explain what these giant cryptic errors mean and how to fix them, i would greatly appreciate it.
A class can have non-static member functions, which operate on individual instances of the class. class CL { public: void member_function() {} }; These functions are called on an instance of the class, like so: CL instance; instance.
Static member functions are allowed to access only the static data members or other static member functions, they can not access the non-static data members or member functions of the class.
But when we try to call Non static function i.e, TestMethod() inside static function it gives an error - “An object refernce is required for non-static field, member or Property 'Program. TestMethod()”. So we need to create an instance of the class to call the non-static method.
A static method can access only static members and can not access non-static members. A non-static method can access both static as well as non-static members.
A class can have non-static member functions, which operate on individual instances of the class. These functions are called on an instance of the class, like so: They can be defined either inside or outside the class definition; if defined outside, they are specified as being in the class' scope.
A non-static member function may be declared virtual or pure virtual. See virtual functions and abstract classes for details. A non-static member function can be declared to take as its first parameter an explicit object parameter, denoted with the prefixed keyword this .
Any function declarations are allowed, with additional syntax elements that are only available for non-static member functions: pure-specifiers, cv-qualifiers, ref-qualifiers, final and override specifiers (since C++11), and member initialization lists . 1) For an object of type X using the class member access operator
During overload resolution, non-static member function with a cv-qualifier sequence of class X is treated as follows: no ref-qualifier: the implicit object parameter has type lvalue reference to cv-qualified X and is additionally allowed to bind rvalue implied object argument
I think the problem you are having is that a member function requires not only a function pointer, but a pointer to the calling object. In other words, member functions have an additional implicit argument that is the pointer to the calling object.
To set a member function to a std::function, you need to use std::bind like this:
std::function<void(int)> testFunc(std::bind(&A::func, this, _1));
This binds the this pointer of the current A instance to the function so it has the function pointer and the object instance, which is enough information to properly call the function. The _1 argument indicates that the first explicit argument will be provided when the function is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With