从功能指针调用方法
call method from a function pointer
i不能用C 11编译。我可以以另一种形式写入,但是我只想使用该代码进行校正,或者我想要一个具有非静态Check2函数的解决方案。
#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;
};
错误: if(((i( ->*(命令:: check(((i->模式,结果((返回exit_success;
没有静态: bool check2_true(const string&amp; atterty,const string&amp; value({return true;}
感谢所有
check
不是Command
的静态成员 - 它是*i
的成员 - 因此您应该使用普通的成员访问语法,i->check
。
另外,您可以在 *this
上调用成员函数的If
实例。
(this->*(i->check))(i->pattern, result)
请注意,(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
S和std::
-PREFIX(
首先,您正在尝试使用类成员Command::check
,但是(可能(有多个Command
-Objects,每个对象都有自己的check
-Member。您必须指定要访问的Command
对象。
由于您使用i
-Eiterator循环通过Command
-Objects的向量循环,因此我假设您要访问i
所引用的任何check-member
。这样:i->check
第二:check
指向方法check_true
,该方法是If
类的成员。这意味着在调用check_true
时,您还必须指定该方法应采取的If
对象(成员check
不包含该信息(
通常这意味着直接调用check_true
时,您会这样这样做:
a.check_true(..) // `a` is of type `class If`
或 b-> check_true(..(// b
是类型class If *
通过函数指针间接执行(在您的情况i->check
(时,您会这样做:
a.*(i->check)(...) // `a` is of type `class If`
或
b->*(i->check)(...) // `b` is of type `class If *`
但是您是这样称呼的:
(i)->*....
和i
不是指向If
对象的指针,而是对Command
对象的引用。
由于您在If::modify()
内部执行所有操作,因此我还假设(i->check)(..)
应该在相同的If
-Object上起作用,因为modify()
当前正在作用。因此,校正的函数调用应为:
this->*(i->check)(i->pattern, result)
- 如何强制从重写方法调用重写的方法基方法?
- C++:使用方法调用析构函数的顺序是什么?
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- 指向类方法调用的指针
- 如何使用 SFINAE 在方法调用中有条件地定义变量?
- 是否有可以处理方法调用依赖关系的设计模式?
- 如何缩短C++中的方法调用?
- 从部分专用模板方法调用模板非静态方法
- 有没有办法禁止派生类中的基类方法调用?
- 为什么这C++只在编译器上编码一个不明确的方法调用Microsoft?
- 从父方法调用子方法
- 如何将子方法调用到父方法
- 虚拟函数在哪里使用 vpointer to vtable 来解析方法调用,非虚拟方法存储在哪里以及如何解析它们?
- 从静态方法调用静态函数指针
- 从同一类中的另一个方法调用方法时出错
- 方法调用意外地像 l 值一样起作用
- 无法从派生的一个方法调用基类方法
- 从类方法调用命名空间中名为 Same 的函数时,重载解析失败
- C 多线程JAVA JNI方法调用