C++ 从函数指针调用方法
我不能用c++11编译。我可以用另一种形式写,但我希望这段代码只更正行错误,或者我想要一个带有非静态check2函数的解决方案C++ 从函数指针调用方法,c++,function-pointers,C++,Function Pointers,我不能用c++11编译。我可以用另一种形式写,但我希望这段代码只更正行错误,或者我想要一个带有非静态check2函数的解决方案 #include <functional> #include <string> #include <vector> using namespace std; class If { public: struct Command { strin
#include <functional>
#include <string>
#include <vector>
using namespace std;
class If {
public:
struct Command {
string pattern;
bool (If::*check)(const string&, const string&);
function<bool(const string&, const string&)> check2;
};
If() {
Command command;
command.check = &If::check_true;
command.check2 = this->check2_true;
m_commands.push_back(command);
}
int modify() {
string result;
for (auto i = m_commands.begin(), end = m_commands.end(); i != end; ++i) {
if (((i)->*(Command::check))(i->pattern, result)) return EXIT_SUCCESS; // ERROR
if (this->*(i->check2)(i->pattern, result)) return EXIT_SUCCESS; // OK but i don't wont static function
}
return EXIT_FAILURE;
}
bool check_true(const string& pattern, const string& value) { return true; }
static bool check2_true(const string& pattern, const string& value) { return true; }
private:
vector<Command> m_commands;
};
错误:
如果i->*命令::checki->pattern,结果返回EXIT\u SUCCESS
无静电:
bool check2_trueconst字符串和模式,const字符串和值{return true;}
谢谢大家check不是命令的静态成员-它是*i的成员-因此您应该使用正常的成员访问语法i->check。
另外,您可以在其上调用成员函数的If实例是*this
请注意,此->*i->check中的外括号是必需的,而内括号不是必需的,但我认为内括号使它更具可读性
您可以使用类型别名、函数和范围循环进一步提高可读性:
class If
{
public:
using CommandFunction = bool (If::*)(const string&, const string&);
// Alternative: typedef bool (If::*CommandFunction)(const string&, const string&);
struct Command {
string pattern;
CommandFunction check;
};
If() {
Command command;
command.check = &If::check_true;
m_commands.push_back(command);
}
bool call(CommandFunction f, const string& a, const string& b)
{
return (this->*f)(a, b);
}
int modify() {
string result;
for (const auto& i: m_commands) {
if (call(i.check, i.pattern, result))
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
bool check_true(const string& pattern, const string& value) { return true; }
private:
vector<Command> m_commands;
};
除了缺少include和std::-前缀之外,代码还有两个问题 首先,您尝试使用class member命令::check,但是可能有多个命令对象,每个对象都有自己的check成员。必须指定要访问的命令对象 由于您使用i迭代器循环遍历命令对象的向量,因此我假设您希望访问我引用的任何对象的check成员。像这样:我->检查 第二:check指向check_true方法,它是If类的一个成员。这意味着在调用check_true时,还必须指定该方法应该对成员检查执行的If对象不包含该信息 通常,这意味着当直接调用check_true时,您可以这样做:
a.check_true(..) // `a` is of type `class If`
a.*(i->check)(...) // `a` is of type `class If`
或
b->检查_true..//b为类型类别,如果*
在您的案例中,通过函数指针间接执行此操作时,i->check按如下方式执行:
a.check_true(..) // `a` is of type `class If`
a.*(i->check)(...) // `a` is of type `class If`
或
但你这样称呼它:
(i)->*....
i不是指向If对象的指针,而是对Command对象的引用
由于所有这些都是在If::modify中完成的,因此我还假设I->check。。如果修改对象当前正在作用,则应作用于同一对象。因此,正确的函数调用应该是:
this->*(i->check)(i->pattern, result)
请包括一个和完整的错误消息。顺便问一下,为什么要使用函数指针?只有一个函数可以指定给Command::checkERROR不是一个有用的问题描述。错误是不编译。我有很多函数,这取决于您的解决方案是否在cpp.sh上编译,使用c++11或c++14很棒!!!这很有效。我的编译器无法识别使用。。但好吧。是否有可能消除调用函数?@MarioRollino是的,您可以消除调用-它所做的只是->*i->checki->pattern,result-但我个人发现很难解码成员指针解引用代码。@MarioRollino如果您的编译器不支持使用新标准C++11,则使用typedef。请参阅编辑。您的解决方案不使用c++11或c++14在cpp.sh上编译此->*i->checki->patter,结果有效。谢谢