I’m trying to get something like this to work by passing a function pointer. I know you can just pass a pointer of the first class to the second and get the second to fire a member function of the first class through the pointer. But I don’t want the second class to rely on knowing who the first class is. This is more like coding style i’m looking for to achieve this. Thanks
//////////////////////////////////////////////////
class Second
{
public:
Second::Second(void (*SecondTriggered)(void));
};
Second::Second(void (*SecondTriggered)(void))
{
SecondTriggered();
}
//////////////////////////////////////////////////
class First
{
public:
First::First();
void SecondTriggered();
Second *second;
};
First::First(){
printf("first class was created");
second = new Second(SecondTriggered);
}
void First::SecondTriggered(){
printf("second class was created and responded");
}
/////////////////
int main()
{
First *first = new First();
}
I get this error:
error C3867: 'First::SecondTriggered': function call missing argument list;
use '&First::SecondTriggered' to create a pointer to member
Any ideas.
You are trying to pass a non-static class member where a standalone function is expected. To do what you are attempting, you would have to do something like the following instead: