简体   繁体   English

如何在模板 function 中执行 function 指针?

[英]How can I execute a function pointer inside a template function?

I want a function that can be called inside of another class which has a single input parameter of type void function pointer.我想要一个 function 可以在另一个 class 内部调用,它有一个类型为 void function 指针的输入参数。 Inside the function, the function pointer should execute the function to which it's pointing.在 function 内部,function 指针应该执行它指向的 function。

// inside TestClass.h
class TestClass
{
public:
    template<class UserClass>
    static void TestingFctPtrStatic(void (UserClass::* TestFunction)(void));
};

template<class UserClass>
inline void TestClass::TestingFctPtrStatic(void(UserClass::* TestFunction)(void))
{
    TestFunction();
}

Calling the function like this gives me the following compile error:像这样调用 function 会出现以下编译错误:

TestClass::TestingFctPtrStatic<TestClass>(&TestClass::PrintStuff);

With PrintStuff():使用 PrintStuff():

void TestClass::PrintStuff()
{
    std::cout << "Printing stuff!" << std::endl;
}

: error C2064: term does not evaluate to a function taking 0 arguments: message: see reference to function template instantiation 'void TestClass::TestingFctPtrNonStatic(void (__cdecl TestClass::* )(void))' being compiled : error C2064: term does not evaluate to a function taking 0 arguments: message: see reference to function template instantiation 'void TestClass::TestingFctPtrNonStatic(void (__cdecl TestClass::* )(void))' 正在编译

void(UserClass::* TestFunction)(void)

This is not a pointer to a function. It is a pointer to a class method.这不是指向 function 的指针。它是指向 class 方法的指针。

TestFunction();

You can't just call a class method out of thin air.您不能凭空调用 class 方法。 You need an object, an instance of the class, whose method gets called.您需要一个 object,它是 class 的一个实例,它的方法被调用。 If p is a pointer to a UserClass , in other words:如果p是指向UserClass的指针,换句话说:

UserClass *p=(points to somewhere);

then you would invoke its method using this syntax:然后您将使用以下语法调用它的方法:

(p->*TestFunction)();

It is unclear from your question's description where the object whose method gets invoked comes from, so you need to figure it out, yourself, based on all the other information you have but was not included in the question.从你的问题描述中不清楚 object 的方法被调用的来源,所以你需要根据你拥有但未包含在问题中的所有其他信息自己弄清楚。 In some form or fashion you'll need to find a UserClass object, somewhere -- either passed in as an additional parameter, or it's lying around, somewhere, in the general vicinity -- and then you can use the TestFunction pointer to invoke this object's method.以某种形式或方式,您需要在某处找到一个UserClass object——要么作为附加参数传入,要么位于附近某处,然后您可以使用TestFunction指针调用它对象的方法。

This is fundamental to C++, provided that this is a class method.这是 C++ 的基础,前提是这是一个 class 方法。 If this PrintStuff is a static class method, then this would be, indeed, a plain old function pointer:如果这个PrintStuff是一个static class 方法,那么这确实是一个普通的老式 function 指针:

void (*TestFunction)();

And it would get invoke, via a pointer, no differently than any other function.它会通过指针被调用,与任何其他 function 没有什么不同。

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

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