1

I have a solution with many projects.
I would like to make a class inside common project that receive array and pointer to function.
This class is a member in other projects on that solution. each project class can hold it as member.

On the constructor of the class inside common, How do i pass the pointer of the function ? how do i set a member inside the common class that holds the location of the function i would like to invoke later on ?

My goal is that i can invoke the function using the pointer - while im in the code of the class that is found in the common project..

Thanks

ilansch
  • 4,784
  • 7
  • 47
  • 96

1 Answers1

2

On the constructor of the class inside common, How do i pass the pointer of the function ? how do i set a member inside the common class that holds the location of the function i would like to invoke later on ?

typedef void(*FUNCPTR)(int);

This defines my function pointer type, but it also restricts the function to the signature below (retuens void and takes 1 int param) you need to define this for your function signature.

void myFunction( int someParam )
{
    //DoSomething with someParam
}


class Foo
{
private:
    FUNCPTR mFunction; //Holds pointer

public:
    Foo( FUNCPTR function) : mFunction(function) {} //Initialise instance

    void callFunc() {mFunction(1);} //call function
};

int main(unsigned int argc, const char** argv)
{
    Foo myFoo(myFunction);

    myFoo.callFunc();
}

In C++ you might well be better however thinking about using function objects in which you create an object and define an operator() which takes the correct parameters and return type but would allow you all the flexibility of objects and polymorphism....

See C++ Functors - and their uses for some discussion and detail

Community
  • 1
  • 1
Caribou
  • 2,070
  • 13
  • 29