array of function pointer pointing to functions of a different class

I have a class MyClass. In it, I want to create an array of function pointer and initialize the array with the functions of another class (MemberClass). MyClass has a pointer to MemberClass as member. but I get this compile time error error C2440: 'initializing' : cannot convert from 'void (__thiscall MyMemberClass::* )(void)' to 'F'

//This class has the function that I want to create a function pointer to
class MemberClass
{

private:

int myValue1;
int myValue2;

public: 

int GetValue1() //I want to point to this function from a function pointer in another class
{

    return myvalue1;
}

int GetValue2() //I want to point to this function from a function pointer in another class
{

    return myvalue2;
}


}




//This has array of function pointer that I want to point to member function of another class

Class MyClass
{

typedef void(*F)();


private:
MemberClass* mclass;
F[] f;
Process();
}

.cpp

MyClass::MyClass()
{
f[2] = {&MemberClass::GetValue1, &MemberClass::GetValue2} //This line throws error

//error C2440: 'initializing' : cannot convert from 'void (__thiscall MyMemberClass::* )(void)' to 'F'

}

void     MyClass::Processing()
{

//This is how I am hoping to use the function pointer array
F[Index]();

}

Answers


F is declared as pointer to function with no parameters returning void. But your functions return int, and are member functions of MemberClass rather than plain ordinary functions. So the type you need is

typedef int (MemberClass::*F)();

Calling it is also more interesting:

int result = (mclass->*f[index])();

Suggestion: rather than a method pointer, use C++11's functional library.

I'm butchering OP's sample code slightly to simplify the example.

MemberClass stays mostly the same. I removed the member variables because the methods are now hard-coded to return 1 and 2 to make them easy to tell apart.

#include <iostream>
#include <functional>

class MemberClass
{
public:
    int GetValue1()
    {
        return 1;
    }

    int GetValue2()
    {
        return 2;
    }
};

myClass gets a rip-up because this is where the action is.

class MyClass
{
private:

I'm using an array of std::function instead of a typedef and an array of the typedef. Note the template argument int(). This is an array of functions that takes nothing and returns an int. Magic in std::bind will provide the hidden this parameter required by methods. If the function has parameters that are not known at the time of binding, use std::placeholders to save room to them in the method's parameter list.

Since the methods are bound to their object, there is no longer any need to store MemberClass* mclass;

    std::function<int()> f[2]; 

public:

Calling the function is simple: index the array and stick the brackets on.

    int Process(int index)
    {
        return f[index]();
    }

The constructor is either a bit trickier, or less tricky, depending on your school of thought. I'm using an initializer list because it is cleaner (to me, at anyrate) and often has performance advantages. For one thing, you can swap out the array for a std::vector or most other containers without having to change a line of code other than the variable definition.

f[0] = std::bind(&MemberClass::GetValue1, mem); 
f[1] =... 

inside the body of the constructor is still an option.

    MyClass(MemberClass * mem):
        f{std::bind(&MemberClass::GetValue1, mem), 
          std::bind(&MemberClass::GetValue2, mem)}
    {
    }

};

And a silly little bit of test code to make sure this all works. Why? Because every time you don't test code in it's simplest form you're taking an unnecessary risk. If it doesn't work small, it won't work big.

int main()
{
    MemberClass c;
    MyClass d(&c);
    std::cout << d.Process(0) << std::endl;
    std::cout << d.Process(1) << std::endl;
}

All together for one cut and paste-able block:

#include <iostream>
#include <functional>

class MemberClass
{

public:

    int GetValue1()
    {

        return 1;
    }

    int GetValue2()
    {

        return 2;
    }

};

class MyClass
{

private:
    std::function<int()> f[2];
public:
    int Process(int index)
    {
        return f[index]();
    }

    MyClass(MemberClass * mem):
        f{std::bind(&MemberClass::GetValue1, mem), 
          std::bind(&MemberClass::GetValue2, mem)}
    {
    }

};

int main()
{
    MemberClass c;
    MyClass d(&c);
    std::cout << d.Process(0) << std::endl;
    std::cout << d.Process(1) << std::endl;
}

Need Your Help

Value Randomization / Scaling Utilities for iPhone?

objective-c random

I work with a lot of random numbers to create constantly shifting textures. I'm getting tired of doing things like: