Hello,
I've a question concerning pointers to functions.
In my self made menu class a function should be called, if the user clicks on a part of the menu. Right now this is working, if the function is global, but I would like to use the code to work inside other classes, too. But then an error occurs. Here are some snippets out of the application:
//Constructor of InterfaceItem Class
//------------------------------------------------------------------------------
machineInterfaceItem::machineInterfaceItem(machineInterfaceItem *_parent,
std::string _name,
int _iItem,
int _nItems,
void (*_fcn_handle)(void)){
//------------------------------------------------------------------------------
parent = _parent;
child.clear();
ClickFunction = (*_fcn_handle);
...
//instantiating machineInterfaceItem in other class
...
menu.push_back(new machineInterfaceItem(NULL,"Move",1,8,cbMove));
...
cbMove is member function of the other class
The compiler error says: no function call for machineInterfaceItem::machineInterfaceItem(NULL, const char [5], int, int, <unresolved overloaded function type>)
I think this is because machineInterfaceItem is not able to call cbMove out of the other class, because the other class is not known in machineInterfaceItem. Is there a smart way to announce the calling class in the interfaceItem class? Maybe by forwarding a pointer to the calling class? Are there other ways? Code examples welcome.
Cheers
Vater
If cbMove a method? Pointers to methods have a different syntax.
For example, T A::f(T2);
A pointer to a function like that would be T(A::*)(T2)
The function should be static to be callable without an object, and the class should be visible to the function taking the pointer.
Then you will have to change your machineInterfaceItem object to
take a boost::function<void(void)> const& as the callback parameter
instead of void (*)().
I tried the boost library. After some trials and testing - it seems that c++ is not able to handle this special problem without effort - I have a working body now. Nevertheless I would like to mention the callback function directly inside the constructor to reduce the amount of additional LOC in the calling routine, maybe in a later step...
O_o. Implementing this in my class produces another error. It seems, that binding myClick to Item class object menu creates another instance/object/whatever. While debugging through my code it seems, that the adress of Test in Line 34 is different to the adress of Test in line 48. Therefore all members of Test are unknown at the time myClick is evaluated in ExecuteCallback.
Any clue??