将void * user_data转换为对象 [英] Casting void *user_data to object

查看:484
本文介绍了将void * user_data转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 void * something 转换为标准C ++中的对象?
特别地,我想要把 void * userdata
转换为 std :: map< String,void *> code>



这可能吗?我尝试:

  // void * user_data是这个函数的一个参数(回调)
std :: map < String,void *> user_data_n; // this line is ok
user_data_n = static_cast< std :: map< String,void *>>(* user_data); //我在这里得到错误。

错误:

 code> Spurious'>>'user'>'终止模板参数列表
预期'>'before'('token
'void *'不是一个指针, to-object type

还是有更好的方法来携带关于调用对象的信息和一些其他参数我可以传递给 void * user_data



UPDATE: b
$ b

@aaa carp建议的屁股我已将>> 更改为>> 和前两个错误被解决了最后是奇怪的,为什么我在这里投入这种类型的消息,而不是当设置回调时放置该对象?

  std :: map< String,void *> user_data_h; 
user_data_h [Object] = this; //这是MainController对象
user_data_h [h] = h; // h被定义为int * h
createTrackbar(trackbar_H,winName,h,255,trackbar_handler,& user_data_h);



其中createTrackbar定义为:

  int createTrackbar(const string& trackbarname,const string& winname,
int * value,int count,TrackbarCallback onChange,void * userdata);

UPDATE2:



这样做解决了我的问题,但遵循同样的方法,为什么我仍然得到错误,当试图投射我的地图对象中包含的对象时?

  void trackbar_handler(int value,void * user_data){
std :: map< String,void *> * user_data_map;
user_data_map = reinterpret_cast< std :: map< String,void *> *>(user_data); //工作! ;)

MainController * controller; //相同的类类型我使用上面的this
controller = reinterpret_cast< MainController *>(user_data_map [Object]); // ERROR here

int * var = reinterpret_cast< int *> (user_data_map [h]); //错误也在这里


解决方案

a void * ,你的结果将是一个指针。所以地图声明应该是:

  std :: map< String,void *> * user_data_n; 

其次,您应该使用 reinterpret_cast (潜在危险)强制转换:

  user_data_n = reinterpret_cast< std :: map< String,void * *>(user_data); 

更新



正如其他人所建议的,您也可以直接使用 static_cast


为什么我在这里投射这种类型的消息,而不是当设置回调时放置那个对象?


任何指针隐式转换为 void * ,但是当将其转换回某个特定类型的指针时,需要显式转换。


为什么在尝试投射我的地图对象中包含的对象时仍然会出错?


如注释中所述,您需要在使用地图对象之前取消引用指针。您可能想要定义一个引用,以使事情变得更容易:

  std :: map< String,void * & user_data_map = 
*(static_cast< std :: map< String,void *>(user_data));


how do I cast void *something to an object in standard C++? Specifically I want want to cast void *userdata to std::map<String, void*>

Is this possible? I am trying:

//void *user_data is a parameter of this function (callback)
std::map <String, void*> user_data_n; //this line is ok
user_data_n = static_cast<std::map<String, void *>>(*user_data); //I get the errors here.

ERRORs:

Spurious '>>' user '>' to terminate a template argument list
Expected '>' before '(' token
'void *' is not a pointer-to-object type

or is there a better way to carry information about the caller object and some other parameters I can pass to void *user_data?

UPDATE:

Ass suggested by @aaa carp I changed >> to > > and the first two errors were solved. The last is strange, Why do I get that kind of message when casting it here and not when putting that object when setting the callback?

std::map<String, void*> user_data_h;
user_data_h["Object"] = this; //this is a MainController object
user_data_h["h"] = h; //h was defined as int *h
createTrackbar("trackbar_H", winName, h, 255, trackbar_handler, &user_data_h);

where createTrackbar is defined as:

int createTrackbar( const string& trackbarname, const string& winname,
int* value, int count, TrackbarCallback onChange, void* userdata);

UPDATE2:

doing this solved my problem but following the same approach, why I still get error when trying to cast objects contained in my map object?

void trackbar_handler(int value, void *user_data){
std::map <String, void*> *user_data_map;
user_data_map = reinterpret_cast<std::map<String, void *> *>(user_data); //WORKED!! ;)

MainController *controller; //the same class type I put using "this" above
controller = reinterpret_cast<MainController *>( user_data_map["Object"]); //ERROR here

int *var = reinterpret_cast<int*> (user_data_map["h"]); //ERROR also here

解决方案

When you're casting from a void *, your result will be a pointer too. So the map declaration should be:

std::map <String, void*> *user_data_n;

Second, you should use reinterpret_cast for such (potentially dangerous) casts:

user_data_n = reinterpret_cast<std::map<String, void *> *>(user_data);

Update:

As others suggested, you could simply use a static_cast as well.

Why do I get that kind of message when casting it here and not when putting that object when setting the callback?

Any pointer can be implicitly converted to void *, but when converting it back to a pointer of some specific type, you need an explicit cast.

why I still get error when trying to cast objects contained in my map object?

As already mentioned in the comments, you need to dereference the pointer before using the map object. You might want to define a reference instead to make things easier:

std::map <String, void*> &user_data_map =
    *(static_cast<std::map<String, void *> *>(user_data));

这篇关于将void * user_data转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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