简体   繁体   English

如果我们知道参数和返回类型,如何使用其指针调用任何类函数?

[英]How to call any class function using its pointer if we know arguments and return type?

How to create a function pointer to any class function knowing only arguments and return type?如何创建一个指向任何只知道参数和返回类型的类函数的函数指针? How to call this function later?以后怎么调用这个函数?

I read about std::function, however I do not have an idea how to implement it without using specific class name like " std::function<void(const ClassName&, int)> f_add_display = &ClassName::func; "我读过 std::function,但是我不知道如何在不使用特定类名的情况下实现它,例如“ std::function<void(const ClassName&, int)> f_add_display = &ClassName::func;

The example below is NOT for compiling, it is only to show the idea I mean:下面的例子不是为了编译,它只是为了展示我的意思:

class collection {
    p* ...; //pointer to any(!) class function with known arguments and return type
} _collection;

class One {
public:
    ...
    bool Foo_1(int, int) {};
    void saveFuncAddr() {_collection.p = this::Foo_1};
};

class Two {
public: 
    bool Foo_2(int, int) {};
    void saveFuncAddr() {_collection.p = this::Foo_2};
};

int main() {
    one* = new One();
    one->saveFuncAddr();
    bool res1 = (*_collection.p)(1, 2);

    two* = new Two();
    two->saveFuncAddr();
    bool res2 = (*_collection.p)(1, 2);
}

First, identifiers beginning with an underscore is reserved in the global namespace , so you shouldn't declare a name like _collection .首先,以下划线开头的标识符在全局命名空间中保留,因此您不应声明像_collection这样的名称。

You can use lambdas to wrap your member functions:您可以使用 lambdas 来包装您的成员函数:

#include <functional>

struct Collection {
    std::function<bool(int, int)> p; 
} collection;

class One {
public:
    bool Foo_1(int, int) {return true;}
    void saveFuncAddr() 
    {
        collection.p = [this](int a, int b){return this->Foo_1(a, b);};
    }
};

class Two {
public: 
    bool Foo_2(int, int) {return true;}
    void saveFuncAddr() 
    {
        collection.p = [this](int a, int b){return this->Foo_2(a, b);};
    }
};

int main() {
    auto one = new One();
    one->saveFuncAddr();
    bool res1 = (collection.p)(1, 2);
    delete one;

    auto two = new Two();
    two->saveFuncAddr();
    bool res2 = (collection.p)(1, 2);
    delete two;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从指针调用具有 std::variant 返回类型和 arguments 的 function? - How to call a function with std::variant return type and arguments from a pointer? 如何将函数指针(我们不知道参数的函数)声明为类成员? - How to declare a function pointer (function that we don't know the arguments) as a class member? 如何从包含基类指针的容器中调用派生类函数(基于其类型)? - How to call derived class function (based on its type) from a container which contain pointer of base class? 有没有办法在类指针类型的指针中调用类操作符而不使用*? - Is there any way to call class operators with out using * , in a pointer to class type? Function 指针,具有“模板化”返回类型和 arguments - Function pointer with “templated” return type and arguments 函数可以返回指向自己类型的指针吗? - Can a function return a pointer to its own type? 传递重载函数指针及其参数时的错误类型推导 - Bad type deduction when passing overloaded function pointer and its arguments 函数是否有可能使用其参数返回指针? - Is it possible that a function return a pointer using its parameters ? 如何调用类指针函数 - How to call a class pointer function 如何调用函数的类的指针? - how to call function to pointer of a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM