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]();
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);
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 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]();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With