0

I have a class in which i want to have a member variable for a method:

template<typename T>
class ParamTest : public Action<T> {
public:
    typedef void (*mixfunctype)(const T &t1, const T &t2, T &t3, double dParam);
    mixfunctype curFunc;
    void setMode(int iMode);
    ...
protected:
    void test1(const T &t1, const T &t2, T &t3, double dParam);
    void test2(const T &t1, const T &t2, T &t3, double dParam);
    ...
}

and in the code

template<typename T>
void ParamTest<T>::setMode(int iMode) {
    if (iMode == 0) {
        curFunc = &test1;
    } else {
        curFunc = &test2;
    }
} 

But the compiler doesn't like this

error: cannot convert ‘void (ParamTest<int>::*)(const int&, const int&, int&, double)’ to ‘ParamTest<int>::mixfunctype’ {aka ‘void (*)(const int&, const int&, int&, double)’} in assignment
     curFunc = &test1;

How should i declare the variable curFunc, and how should i assign a function to it, to have compilable code?

user1479670
  • 1,145
  • 3
  • 10
  • 22
  • 1
    I created a live example for what you asked [godbolt](https://godbolt.org/z/mLkddV). You have to use: `typedef void(ParamTest::*mixfunctype)(const T &t1, const T &t2, T &t3, double dParam);` and in `setMode` you need to write `curFunc = &ParamTest::test1`. – StefanKssmr May 12 '20 at 11:58
  • Thanks that worked. For completeness, her is how to call the function from inside the class: (this->*curFunc)(tMother, tFather, tChild, dParam); – user1479670 May 13 '20 at 06:37

0 Answers0