【发布时间】:2010-03-29 15:25:30
【问题描述】:
我正在使用哈希表(Google Inc 的源代码)来存储一些定义为的方法指针:
typedef Object *(Executor::*expression_delegate_t)( vframe_t *, Node * );
显然“执行者”是类。
向哈希表插入一些值的函数原型是:
hash_item_t *ht_insert( hash_table_t *ht, ulong key, ulong data );
所以基本上我正在做插入双转换方法指针:
ht_insert( table, ASSIGN, reinterpret_cast<ulong>( (void *)&Executor::onAssign ) );
table 在 Executor 类的声明中定义为“hash_table_t *”,ASSIGN 是无符号长整型值,“onAssign”是我必须映射的方法。
现在,Executor::onAssign 存储为一个无符号长整型值,我认为它在内存中的地址,我需要将 ulong 转换回方法指针。但是这段代码:
hash_item_t* item = ht_find( table, ASSIGN );
expression_delegate_t delegate = reinterpret_cast < expression_delegate_t > (item->data);
给我以下编译错误:
src/executor.cpp:45: error: invalid cast from type ‘ulong’ to type ‘Object* (Executor::*)(vframe_t*, Node*)’
我在 x86 GNU/Linux 机器上使用 GCC v4.4.3。
有什么提示吗?
【问题讨论】:
-
你为什么使用无类型的 C 哈希表实现而不是正确类型的 C++ 类?