简体   繁体   English

如何在C ++中将const FB :: variant&转换为用户定义的类?

[英]How to cast const FB::variant& into user defined class in c++?

I am tryintg to cast const FB::variant& sample into SampleJS* in C++. 我正在尝试将const FB :: variant&sample转换为C ++中的SampleJS *。 like this: 像这样:

SampleJS* info = sample.cast<SampleJS*>();

i do not know what is going wrong here. 我不知道这里出了什么问题。 this gives me error of: 这给了我以下错误:

boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_any_cast> > 

Thank you in advance. 先感谢您。

You need to understand that FB::variant just stores whatever type is put into it. 您需要了解FB :: variant仅存储放入其中的任何类型。 Your code will absolutely work... if what is inside happens to be a MouseInfoJS*. 如果内部恰好是MouseInfoJS *,那么您的代码将绝对有效。 However, you'd have to go through a lot of work to put something like that inside, since the FB::variant class is designed to make it difficult, but not impossible, to store inside it types that it doesn't know. 但是,由于FB :: variant类的设计使其难以(但并非不可能)将未知类型存储在其中,因此您必须进行大量工作才能将类似内容放入其中。

So basically, what you're trying to do probably doesn't make any sense, so you can't do it. 因此,基本上,您尝试做的事情可能没有任何意义,因此您无法做到。

Depending on what type MouseInfoJS is, it might sorta make sense. 根据MouseInfoJS的类型,它可能有点合理。 Does MouseInfoJS inherit from JSAPI? MouseInfoJS是否继承自JSAPI? (or JSAPIAuto?) If so, then it would somewhat make sense to try what you're doing, except that you'd never have a MouseInfoJS*, you'd have a MouseInfoJSPtr, which would be a typedef for boost::shared_ptr, since it would then be partially owned by the page and thus dangerous to store a raw pointer there. (或JSAPIAuto?)如果是这样,那么尝试您正在做的事情在某种意义上是有意义的,除了您永远不会拥有MouseInfoJS *之外,您将拥有MouseInfoJSPtr,这对于boost :: shared_ptr来说是typedef ,因为它随后将部分归页面所有,因此在其中存储原始指针很危险。

Even in this case, which you'd need to use .convert_cast<MouseInfoJSPtr> , not .cast , it won't work on most modern browsers because they wrap the NPObject returned by FireBreath in another object which doesn't allow us to get the original object back; 即使在这种情况下,你需要使用.convert_cast<MouseInfoJSPtr>.cast ,它不会在最现代的浏览器,因为他们被包裹在FireBreath另一个对象返回的NPObject工作不允许我们获得原物退回; I believe this is a security feature. 我相信这是一项安全功能。 For more information, see A firebreath JSAPI will not be recognized in firefox 有关更多信息,请参见firefox无法识别firebreath JSAPI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM