Passing function pointers to an objects constructor

OK, I want to make a pointer to a memberfunction of a class one of the arguments in a class's constructor. I do not know how however, here is as far as I have gotten


Function.h

1
2
3
4
5
6
7
8
//this function contains the member function that will be passed as a pointer
#include <iostream>

class Function
{
public:
    void passedFunction(){ std::cout<<"this function is passed as a pointer"; }
};



ReceiveFunction.h

1
2
3
4
5
6
7
8
9
10
11
12
//this function takes a pointer to passedFunction() as an argument


class ReceiveFunction
{
public:
    // I am unclear as to if this is correct or not
    ReceiveFunction( ( * )() );//constructor that takes a pointer to a function

    // pointer in which the function will be stored
    void ( *receivedfunction )();
};



ReceiveFunction.cpp

1
2
3
4
5
6
#include "ReceiveFunction.h"

ReceiveFunction( void ( *tempfunction )() )
    : receivedfunction( tempfunction )
{
}//empty body 



MakeObjects.h

1
2
3
4
5
6
7
8
9
10
11
12
13
// initializes the objects in its constructor
class Function;// forward declarations
class ReceiveFunction;

class MakeObjects
{
public:
    MakeObjects();// constructor

    // make objects
    Function function;
    ReceiveFunction receivefunction;
};



MakeObjects.cpp

1
2
3
4
5
6
7
8
#include "MakeObjects.h"
#include "Function.h"
#include "ReceiveFunction.h"

MakeObjects()
    : receivefunction( &Function::passedFunction )
{
}// empty function 
1
2
3
4
5
6
7
8
9
struct A{
	return_type function(parameter_type);
};

typedef return_type(A::*pointer_to_A_function)(parameter_type);

void function(pointer_to_A_function pfunc){
	//...
}
Topic archived. No new replies allowed.