简体   繁体   English

C ++函数指针

[英]C++ function pointer

Is there a way in C++ to make an "untyed" function pointer ? 在C ++中,有没有一种方法可以制作“未使用”的函数指针? For example: 例如:

// pointer to global function
void foo( void (*fptr)() );

// pointer to member
void foo( void (Bar::*fptr)() );

Is there a way I can remove the class on which the member is ? 有没有办法可以删除该成员所在的类? So that I could do something like this: 这样我可以做这样的事情:

void foo( void ("any type"::*fptr)(), "same type as for the pointer" &instance );

And then, in foo, I would like to store that pointer in a list, so that I can iterator over the list and call the function/member pointed to, regardless of what class it belongs to. 然后,在foo中,我想将该指针存储在列表中,这样我就可以遍历列表并调用所指向的函数/成员,而不管它属于哪个类。 Of course I'd need a list of instances on which to call the function. 当然,我需要在其上调用该函数的实例列表。

Thx. 谢谢。

You can use a template. 您可以使用模板。

template<typename T> void foo( void(T::*)(), T&) { ... }

However, people prefer to go for the function object approach. 但是,人们更喜欢使用功能对象方法。 You can do this dynamically or statically. 您可以动态或静态地执行此操作。

void foo(std::function<void()> func) {
    // std::bind is used to make this out of a member function
}
template<typename T> void foo(T t = T()) {
    t(); // This is the best approach.
}

Edit: Some examples. 编辑:一些例子。

void foo(std::function<void()> func) {
    std::cout << "In example one ";
    func();
}
template<typename T> void foo(T t = T()) {
    std::cout << "In example two ";
    t();
}
class some_class {
public:
    void func() { std::cout << "in ur function!\n"; }
};
int main(void)
{
    some_class* ptr = NULL;
    struct tempfunctor {
        tempfunctor(some_class* newptr)
            : ptr(newptr) {}
        some_class* ptr;
        void operator()() { return ptr->func(); }
    };
    foo(tempfunctor(ptr)); // Calls example two
    foo(std::function<void()>(tempfunctor(ptr))); // Calls example one
    foo(std::function<void()>(std::bind(&some_class::func, ptr)); // I'm not that familiar with bind, it looks something similar to this.
    std::cin.get();
}

This is the idiom called the function object idiom, used heavily in STL and other high-quality libraries. 这是一种称为函数对象惯用语的惯用语,在STL和其他高质量库中大量使用。 The compile-time template is cleaner but the std::function can be bound at runtime. 编译时模板比较干净,但是std :: function可以在运行时绑定。

Edit @ OP: I didn't quite see your list requirement in there. 编辑@ OP:我在那里没有看到您的列表要求。 A std::function<void()> is your best choice here. std::function<void()>是您的最佳选择。

The following seems to work fine with g++ and MSVC: 以下似乎可以在g ++和MSVC上正常工作:

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
using namespace std;

void foo( boost::function<int()> f )
{
    cout << "f() = " << f() << endl;
}

template< class Type >
void foo( int (Type::*f)() const, Type const& o )
{
    foo( boost::bind( f, boost::ref( o ) ) );
}

int func1() { return 1; }
struct S { int func2() const { return 2; } };

int main()
{
    foo( func1 );
    foo( &S::func2, S() );
}

Disclaimer: I seldom use the Boost stuff and I just typed the above without bothering to check the docs, so possibly it could be expressed more cleanly. 免责声明:我很少使用Boost的东西,我只是打了上面的代码而不必费心检查文档,因此可以更清晰地表达它。

Also note that C++0x standard library offers the same functionality. 还要注意,C ++ 0x标准库提供了相同的功能。

Cheers & hth., 干杯,……

No. The bound class is an intrinsic part of the member function pointer type. 否。绑定类是成员函数指针类型的固有部分。

You can, however, use a member function pointer to a common baseclass, or a template. 但是,您可以使用指向通用基类或模板的成员函数指针。

Can you use functors in your list? 您可以在列表中使用函子吗?

http://en.wikipedia.org/wiki/Function_object http://en.wikipedia.org/wiki/Function_object

Have a look at Fast Delegates: http://www.codeproject.com/KB/cpp/FastDelegate.aspx 看看快速代表: http : //www.codeproject.com/KB/cpp/FastDelegate.aspx

This is an easy drop-in library that allows you to delegate pretty much anything and at a very high speed. 这是一个简单的嵌入式库,可让您以很高的速度委派几乎所有内容。

您不能有这样的指针,但是您可以具有boost::any的集合,并将异构指针(或任何类型的仿函数 )放入其中。

template <typename T>
void foo( void (T::*fptr)(), T& instance)
{
    // ...
}

I'm not going to play expert here, but I think this will work, if not I would like to know why. 我不会在这里扮演专家,但是我认为这会奏效,否则我想知道为什么。

You can't do that, and you shouldn't do that even if you could, because it is against the spirit of the language. 您不能这样做,即使可以也不要这样做,因为这违反了语言的精神。 Create a base class with "fptr" as a pure virtual member, and inherit all your classes from that class. 创建一个以“ fptr”作为纯虚拟成员的基类,并从该类继承所有类。

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

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