I have this member defined in
class X
{
private:
int (TMyClass::*pt2Member)(float, char, char);
}
I would like to initialize this member to NULL in constructor. Is it possible this way:
X::X()
{
TMyClass::*pt2Member = NULL;
}
?
What is right syntax? My function is C++ non static.
Then, how should I write the setter?
// declaration
void set_pointer_to_function(int(*pt2func)(float f, char c, char c));
// definition
void X::set_pointer_to_function(int(*pt2func)(float f, char c, char c))
{
pt2Member = pt2func;
}
is it correct?
You need to assign to the pointer itself, not to what it points. By doing what you are doing, you’re attempting to dereference the pointer, which won’t work in this case since it is a method pointer, and to where it points (and thus the side effects from dereferencing it) are undefined since you have not yet assigned to it.
Will therefore work. Also, you can initialise it along with the constructor like follows: