简体   繁体   English

是否可以在C ++中使用参数创建函数指针

[英]Is it possible to make a function pointer with parameters in C++

I'm trying to capture a function pointer to hand to a functor and I don't understand why I can't. 我正在尝试捕获指向函子的函数指针,但我不明白为什么不能这样做。

Functor Class: 函子类:

template <class C>
class MyFunctor : public BaseFunctor {
  public:
    typedef long (C::*t_callback)(const long, const char *);

    MyFunctor (void) : BaseFunctor(), obj(NULL), callback(NULL) {}
    virtual long operator() (const long a, const char *b) {
        if ( obj && callback ) {
            (obj->*callback)(a,b);
        }
    }

    C *obj;
    t_callback callback;
};

Elsewhere in the code: 在代码的其他地方:

The function signature is long C::Func (const long, const char *) 函数签名为long C::Func (const long, const char *)

MyFunctor funky;
funky.obj = this;
funky.callback = Func;

Then I get the error: 然后我得到错误:

... function call missing argument list; ...

Why does this not work? 为什么这不起作用?

EDIT: In working through the suggestions below, I came across a simple solution to make my particular implementation work. 编辑:通过下面的建议,我遇到了一个简单的解决方案,使我的特定实现工作。

funky.callback = &C::Func;

I'm not 100% sure what your trying to do from your code but two C++ features that are much easier to use than function pointers are std::function and std::mem_fn here's an example of using std::function from the site. 我不是100%知道您要从代码中尝试做什么,但是两个比函数指针更容易使用的C ++功能是std::functionstd::mem_fn这是在网站上使用std::function的示例。

#include <functional>
#include <iostream>

struct Foo {
    Foo(int num) : num_(num) {}
    void print_add(int i) const { std::cout << num_+i << '\n'; }
    int num_;
};

void print_num(int i)
{
    std::cout << i << '\n';
}

struct PrintNum {
    void operator()(int i) const
    {
        std::cout << i << '\n';
    }
};

int main()
{
    // store a free function
    std::function<void(int)> f_display = print_num;
    f_display(-9);

    // store a lambda
    std::function<void()> f_display_42 = []() { print_num(42); };
    f_display_42();

    // store the result of a call to std::bind
    std::function<void()> f_display_31337 = std::bind(print_num, 31337);
    f_display_31337();

    // store a call to a member function
    std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
    Foo foo(314159);
    f_add_display(foo, 1);

    // store a call to a function object
    std::function<void(int)> f_display_obj = PrintNum();
    f_display_obj(18);
}

This is the function pointer in your code: 这是代码中的函数指针:

typedef long (C::*t_callback)(const long, const char *);

To make this an std::funciton do: 要使其成为std::funciton请执行以下std::funciton

std::function<long(const long,const char*)> t_callback = something ; std::function<long(const long,const char*)> t_callback = something ;

You can't assign a function with signature unsigned long Func (const long, const char *) to a variable of type long (C::*)(const long, const char *) because 您不能将具有签名unsigned long Func (const long, const char *)的函数分配给long (C::*)(const long, const char *)类型的变量,因为

  1. One returns unsigned long and the other returns long . 一个返回unsigned long ,另一个返回long
  2. One is a pointer-to-member, the other is the name of a non-member "free" function. 一个是指向成员的指针,另一个是非成员“免费”函数的名称。

If you want Func to simply be called without a C object, then you need to remove C:: from t_callback . 如果希望不使用C对象而直接调用Func ,则需要从t_callback删除C:: If Func is actually a member function and your signature is not copied faithfully in the question ( do not paraphrase code!! ), you must use the :: operator when forming a pointer-to-member value, as in &ThisClass::Func . 如果Func实际上是成员函数,并且没有在问题中如实复制您的签名( 请勿解释代码!! ),则在形成指向成员值的指针时必须使用::运算符,如&ThisClass::Func

The peculiar error you see is due to overload resolution being performed on the name. 您看到的特殊错误是由于对该名称执行了重载解析。 The compiler sees you have something of that name, but it's reporting that none of the things with that name (there could potentially be many) work in the given expression. 编译器看到您有该名称的东西,但是它报告说,该名称中的任何东西(可能有很多)都不能在给定的表达式中工作。


As others have mentioned, this isn't something you should actually use in a project. 正如其他人所提到的,这不是您应该在项目中实际使用的东西。 This kind of functionality is covered by std::function and std::bind , which are available even in C++03 using the standardized TR1 library, so doing it yourself is best kept to a self-contained exercise. std::functionstd::bind涵盖了这种功能,即使在使用标准TR1库的C ++ 03中也可用,因此最好自己进行一次独立的练习。

Without writing any custom code, you can do 无需编写任何自定义代码,您可以

std::function< long( long, char * ) > funky = std::bind( &MyClass::Func, this );

This gets a pointer-to-member to the Func method, attaches the current this pointer to it, then creates an indirect-call wrapper around it all which is valid for the lifetime of this . 这将获得指向Func方法的成员的指针,将当前的this指针附加到该方法,然后围绕该方法创建一个间接调用包装器,这对于this整个生命周期都是有效的。

funky( 3, "hello" );

To avoid the indirect call, use auto to avoid declaring a std::function . 为了避免间接调用,请使用auto避免声明std::function You will get an object of a one-off type. 您将获得一次性类型的对象。 (You can't assign another function to it.) (您不能为其分配其他功能。)

auto funky = std::bind( &MyClass::Func, this );

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

相关问题 是否可以使用NULL检查制作通用的C ++函数指针包装? - Is it possible to make a generic C++ function pointer wrapper with NULL checks? 是否可以使具有多个参数的泛型函数成为C ++类中的朋友? - Is it possible to make a generic function with multiple parameters a friend in a class in c++? C++ 将指针传递给具有引用参数的函数 - C++ passing in pointer to a function with reference parameters 函数参数中的 C++ 数组指针 - C++ Array Pointer in Function Parameters C ++是否可以使用通用函数指针? - C++ Is it possible to have a generic function pointer? 使C ++重载操作符成为函数指针 - Make a C++ overloaded operator a function pointer 我可以在 C++ 中将函数指针分配给带有 const 参数的函数吗? - Can I assign a function pointer to a function with const parameters in C++? Is it possible to assign an C++ pointer to an attribute of a Python object, and retrieve it as a C++ pointer in another C++ function? - Is it possible to assign an C++ pointer to an attribute of a Python object, and retrieve it as a C++ pointer in another C++ function? 是否可以在C ++中使用指向模板函数的函数指针? - Is it possible to have a function pointer to a template function in c++? 将向量传递给具有指针参数的函数的麻烦c ++ - troubles with vector being passed into a function with pointer parameters c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM