Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing functions from class in array and invoke them c++

Tags:

c++

class

i try store a functions(methods) from class in array and use them. The error handle is

In function 'int main()':| 'actions' was not declared in this scope

this my code(i delete unnecessary code) the class.h:

class Calculator
{
    public:
        int num1,num2;
        void (Calculator::*actions[4])();
        void  add();
        void  minuz();
        void  multi();
        void  div();
        Calculator();
};

class.cpp:

void  Calculator::add()
{}
void  Calculator::minuz()
{}
void  Calculator::div()
{ }
void Calculator::multi()
{}
Calculator::Calculator()
{
    actions[0]=add;
    actions[1]=minuz;
    actions[2]=div;
    actions[3]=multi;
}

main:

Calculator cal;
.....
.....
cal.*actions[num]();
like image 966
Naor Malca Avatar asked Dec 07 '22 16:12

Naor Malca


2 Answers

C++ syntax for function pointer declaration is quite complicated, so it better use typedefs To call function by pointer you need extra () around dereferenced function pointer. Finally it will be:

class Calculator
{
    public:
        typedef void (Calculator::*action)();
        int num1,num2;
        action actions[4];
        void  add();
        void  minuz();
        void  multi();
        void  div();
        Calculator();
};

void  Calculator::add()
{}
void  Calculator::minuz()
{}
void  Calculator::div()
{ }
void Calculator::multi()
{}
Calculator::Calculator()
{
    actions[0]=&Calculator::add;
    actions[1]=&Calculator::minuz;
    actions[2]=&Calculator::div;
    actions[3]=&Calculator::multi;
}

int main(int, char**) {
    Calculator cal;
    int num = 0;
    (cal.*cal.actions[num])();
    return 0;
}

for better readability I'd suggest add function Calculator::call_by_index(int):

void Calculator::call_by_index(int index)
{
    (this->*actions[index])();
}

and call it in such way:

cal.call_by_index(num);
like image 91
user2807083 Avatar answered Dec 17 '22 21:12

user2807083


Using a typedef usually helps: (c++03)

Live On Coliru

class Calculator
{
    public:
        int num1,num2;
        typedef void (Calculator::*Action)();
        Action actions[4];
        Calculator() {
            actions[0]=&Calculator::add;
            actions[1]=&Calculator::minuz;
            actions[2]=&Calculator::div;
            actions[3]=&Calculator::multi;
        }
    private:
        void add()   {}
        void minuz() {}
        void multi() {}
        void div()   {}
};

int main() {
    Calculator cal;
    (cal.*cal.actions[1])();
}

C++11 aliases

C++11 makes it easier:

    using Action = void (Calculator::*)();
    Action actions[4];

See also https://isocpp.org/wiki/faq/pointers-to-members#fnptr-vs-memfnptr-types

Live On Coliru

std::function<>

Also in c++11 (or boost if you want it in c++03):

    using Action = std::function<void(Calculator&)>;
    Action actions[4];

Which you would still call like

cal.actions[1](cal);

I'd pre-bind to the Calculator instance:

Live On Coliru

#include <functional>

class Calculator
{
    public:
        int num1,num2;
        using Action = std::function<void()>;
        Action actions[4];
        Calculator() {
            actions[0] = [this]() { add(); };
            actions[1] = [this]() { minuz(); };
            actions[2] = [this]() { multi(); };
            actions[3] = [this]() { div(); };
        }
    private:
        void add()   {}
        void minuz() {}
        void multi() {}
        void div()   {}
};

int main() {
    Calculator cal;
    cal.actions[1]();
}
like image 39
sehe Avatar answered Dec 17 '22 22:12

sehe