我正在尝试使向量保存指向函数的空指针,稍后将连续调用该函数。
所以,假设我有三个函数。 int a()
, void b();
, bool c();
我的向量是 vector; vec;
以及我的存储函数指针的函数。
void registerFunction(void *func)
{
vec.push_back(func);
}
现在我的问题是当尝试调用所有存储的函数时,因为它们都是空指针,所以我无法在不知道它们的类型的情况下调用这些函数。
所以我的问题是...有什么方法可以存储符号类型,以便我可以将它们与各自的指针相关联,然后在调用函数的 void 指针时进行类型转换?
注意:函数并不总是类型,例如void (*)()
,我也想添加方法,因此即。 void (someclass::)()
。是不是要求太多了?应该有效吗?
I am trying to make a vector hold void pointers to functions, which will later be called secuentially.
So, lets say that I have got three functions. int a()
, void b();
, bool c();
My vector is vector<void *> vec;
And my function that stores pointers to functions.
void registerFunction(void *func)
{
vec.push_back(func);
}
Now my problem is when trying to call all the functions stored, since they are all void pointers, I just cannot call the functions without knowing their type.
So my question is... is there any way to store types of symbols so I can relate them to their respective pointers and then typecast when calling a void pointer to a function?
Note: Functions won’t be always be of type, for example, void (*)()
, I will want to add methods also, hence ie. void (someclass::)()
. Is it asking for too much? Should it work?
发布评论
评论(3)
您无法将函数指针转换为
void*
。这是不允许的。如果所有函数都可以使用零参数调用,并且您不关心返回类型,则可以使用
std::vector>
(<如果您的 C++ 标准库不支持,也可以在 Boost 和 TR1 中找到 code>function)。不过,如果我没记错的话,C++11
std::function
实现中不允许返回类型转换,在这种情况下,您可能需要类似以下内容:(在 Boost 实现中,我我知道您可以直接使用
function
,但我很确定 C++11 中不再允许这样做,我可能是错的,希望在评论中予以澄清。 ,如果有人知道的话。)You cannot convert a function pointer to
void*
. It is not allowed.If all of the functions are callable with zero arguments and you don't care about the return type, you can use
std::vector<std::function<void()>>
(function
can also be found in Boost and TR1 if your C++ Standard Library does not support it).Though, if I recall correctly, return-type conversion is not allowed in the C++11
std::function
implementation, in which case you may need something like the following:(In the Boost implementation I know you can use
function<void()>
directly, but I'm pretty sure that is no longer allowed in C++11. I could be wrong and I'd appreciate clarification in the comments, if someone does know.)void*
不能安全地用作通用函数指针类型(尽管您可能可以逃脱它)。但是任何函数到指针的值都可以转换为另一种指针到函数类型并再次转换回来,而不会丢失信息。因此,您可以使用 void (*)()(指向返回 void 且不带参数的函数的指针)作为通用函数指针类型。
至于存储类型,我不确定这是否可能。如果可能性有限,则可以使用枚举类型和 switch 语句。
但使用基于继承的设计可能会更好。
void*
can't safely be used as a generic pointer-to-function type (though you might be able to get away with it).But any function-to-pointer value can be converted to another pointer-to-function type and back again without loss of information. So you can use, for example,
void (*)()
(pointer to function returning void and taking no arguments) as a generic pointer-to-function type.As for storing the type, I'm not sure that's possible. If there are only a limited number of possibilities, you can use an enumeration type and a switch statement.
But you'll probably be better off using a design based on inheritance.
只要所有函数都遵循相同的类型签名,您就可以将 void 指针强制转换回该类型。
更好的解决方案是 typedef 函数类型:
那么您将不需要强制转换。
As long as all the functions follow the same type signature, you can cast the void pointers back to that type.
A better solution is to typedef the function type:
Then you will not need the cast.