Hello this is an update of this previous question i did
I was trying to execute a function by pointer inside a class (the class content is not relevant to the problem I am having).
This was the code posted on the previous question:
error message:
cannot convert ‘Clip::func’ from type ‘void (Clip::)(std::string) {aka void (Clip::)(std::basic_string<char>)}’ to type ‘callback {aka void (*)(std::basic_string<char>)}’
Clip.h
void func(string str){
cout << str << endl;
}
Clip.cpp
void Clip::update(){
typedef void(*callback)(string);
callback callbackFunction = func;
callbackFunction("called");
}
------The solution i found------
Me and a friend did some quick research to find a way to get around this situation and this is what we found. Even though this works, we have some doubts about the expresios involved (questions at the end of the post)
Clip.h
typedef void (Clip::*Callback) (string);
void func(string _val);
Clip.cpp
void Clip::update(){
Callback function;
function = &Clip::func;
(this->*function)("a");
}
Questions:
a- in the previous post @tkausl answered: "...Member function pointers are not compatible with (non-member) function pointers". Is that the reason why the other code does not work inside of any class (even if that class is completely empty)?
b- why is it necessary to declare the pointer like thi: Clip::*Callback if we are already inside of the Clip class?
c- What does this expression mean?: this->*function
d- is there an easier way or expression for this same requierement?
thanks in advance.