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?

2
  • 1
    I created a live example for what you asked godbolt. 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. Commented May 12, 2020 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); Commented May 13, 2020 at 6:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.