预先有自定义类型Object如下:
class Object{
public:
string type;
void* value;
public:
Object(string _type, void* _value) : type(_type), value(_value) {}
};
接着有个函数返回一个变量Object* objP
, 其堆中对应的值:type是string类型的值"map", value
是 map<string, Object*>*
转 void*
类型的指针。然后问题来了,无法把 void*
转回 map
类型从而无法拿到value
对应的值。
试过以下方法都不行:
map<string, Object*>* objMapP = (map<string, Object*>*)objP->value;
map<string, Object*> objMap = *((map<string, Object*>*)ObjP->value);
reinterpret_cast
static_cast
请大神赐教
可以转呀
class Object{
public:
std::string type;
void* value;
public:
Object(std::string _type, void* _value) : type(_type), value(_value) {}
};
int main()
{
std::map<std::string, Object*>* tmp = new std::map<std::string, Object*>();
(*tmp)["hello"] = nullptr;
Object* ptr = new Object("map", tmp);
std::map<std::string, Object*>* p = (std::map<std::string, Object*>*)ptr->value;
auto it = p->find("hello");
assert(it != p->end());
return 0;
}