从功能指针调用方法

call method from a function pointer

本文关键字:方法 调用 指针 功能      更新时间:2023-10-16

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)