简体   繁体   English

将成员函数分配给函数指针

[英]assign a member function to a function pointer

If I have two classes like this :如果我有两个这样的课程:

class A
{
    public:
        int *(*fun)( const int &t );
        A( int *( *f )( const int &t ) ) : fun( f ) {}
};

class B
{
    private:
        float r;

        int *f(const int &t)
        {
            return new int( int( r ) + t );
        }
        A a;
        B() : a( A( f ) ) {}
};

This results in compiler error.这会导致编译器错误。

I want to assign f to a 's function pointer.我想将f分配给a的函数指针。

The thing is that A can be used by many classes not just B so I can't simply define fun as B::*fun .问题是A可以被许多类使用,而不仅仅是B所以我不能简单地将fun定义为B::*fun

None of the posts I've found on the internet and here on stackoverflow address the issue of using the function pointers with many classes each having its own member function but the same prototype.我在互联网上和 stackoverflow 上找到的所有帖子都没有解决将函数指针与许多类一起使用的问题,每个类都有自己的成员函数但原型相同。

So what to do?那么该怎么办?

this result in compiler error这导致编译器错误

Because f is a A 's non-static member function returning int* , that accepts single const reference to int .因为fA的返回int*的非静态成员函数,它接受对int的单个 const 引用。 That means, that its type is not:这意味着,它的类型不是:

int* (*)(const int &);

but:但:

int* (A::*)(const int&);

Also, your code signalizes very bad design - I think you need simple virtual function.此外,您的代码表明设计非常糟糕 - 我认为您需要简单的虚函数。 But if you want to keep writing things this way, you may want to read: ISOCPP: Pointers to members .但是如果你想继续这样写,你可能需要阅读: ISOCPP: Pointers to members

Remember, that non-static member functions of type C always accepts additional implicit argument of type C* (or const C* , if function is declared with const qualifier), that points to instance of C this function was called on.请记住, C类型的非静态成员函数始终接受C*类型的附加隐式参数(或const C* ,如果函数使用const限定符声明),它指向调用此函数的C实例。

Your code looks confusing and, personally, I believe that C function pointers look ugly on C++'s OO implementation .您的代码看起来令人困惑,而且我个人认为C 函数指针C++ 的 OO 实现中看起来很丑陋。 So I would advise you to use the std::function .所以我建议你使用std::function It only has been available since C++11 .它仅从C++11开始可用。 If you cannot use it, try looking on Boost's Implementation .如果您不能使用它,请尝试查看Boost 的 Implementation

I can give you an example of how to use the std::function :我可以给你一个如何使用std::function的例子:

bool MyFunction(int i)
{
    return i > 0;
}

std::function<bool(int)> funcPointer = MyFunction;

Using this you will drastically improve your code reliability.使用它,您将大大提高代码的可靠性。 As of your problem, specifically:至于你的问题,具体来说:

class A
{
public:
    std::function<int*(const int&)> fun;
    A(std::function<int*(const int&)> f) : fun(f) {}
};

class B
{
private:
    float r;
    int *f(const int &t)
    {
        return new int(int(r) + t);
    }
    A *a;
    B()
    {

        std::function<int*(const int&)> tempFun = std::bind(&B::f, this, _1);
        a = new A(tempFun);
    }
};

You have to add the following namespace:您必须添加以下命名空间:

using namespace std::placeholders;

So what to do那么该怎么办

Not much.不多。 Other than templating A on the type of objects for which it will hold a pointer to a member function taking a reference to a const int and returning a pointer to int.除了在对象类型上模板A之外,它将持有指向成员函数的指针,该成员函数获取对 const int 的引用并返回指向 int 的指针。

What you're trying to do is to mix a pointer to a free function with a pointer to member function.您要做的是将指向自由函数的指针与指向成员函数的指针混合在一起。 Whilst it sounds like they're both function pointers they're different enough to not be able to pass through the same type definition.虽然听起来它们都是函数指针,但它们的差异足以无法通过相同的类型定义。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM