如何存储各种类型的函数指针一起? [英] How to store various types of function pointers together?

查看:94
本文介绍了如何存储各种类型的函数指针一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

普通指针可以使用通用无效* 存储。例如。

Normal pointers can be stored using a generic void*. e.g.

void* arr[10];
arr[0] = pChar;
arr[1] = pINt;
arr[2] = pA;

有时回来,我碰到一个讨论中,无效* 可能没有足够的能力来存储一个函数指针没有在所有平台上的数据丢失(比如64位来到和更多)。我不知道这个事实虽然。

Sometime back, I came across a discussion that, void* may not be capable enough to store a function pointer without data-loss in all platforms (say 64-bit and more). I am not sure about this fact though.

如果这是真的,那么什么是存储函数指针的集合最轻便的方法是什么?
[注:<一href=\"http://stackoverflow.com/questions/4699018/how-to-create-a-container-that-holds-different-types-of-function-pointers-in-c\">This问题不令人满意的回答这个问题。]

If that's true, then what is the most portable way to store a collection of function pointers ? [Note: This question doesn't satisfactorily answer this.]

修改:我将存储此函数指针与索引。有每每当这个集合访问索引相关联的类型转换。截至目前,我感兴趣的只是做它的一个数组或矢量]

Edit: I will be storing this function pointers with an index. There is a typecasting associated with every index whenever this collection is accessed. As of now, I am interested only to make an array or vector of it.]

推荐答案

您可以一个函数指针转换为任何函数类型的另一个函数指针和背部无损失。

You can convert a function pointer to another function pointer of any function type and back without loss.

所以只要当你通过函数指针调用,你先强制转换回正确的类型,您可以将所有的函数指针的存储是这样的:

So as long as when you make the call through the function pointer you typecast it back to the correct type first, you can store all of your function pointers in something like:

typedef void (*fcn_ptr)(void);  // 'generic' function pointer

fcn_ptr arr[10];

这篇关于如何存储各种类型的函数指针一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆