简体   繁体   English

将tr1 :: function强制转换为void *

[英]Casting a tr1::function to a void*

I have the following tr1::function prototype that I am trying to cast to a void*: 我有以下tr1 :: function原型,我试图将其转换为void *:

typedef std::tr1::function<cv::Mat (const cv::Mat,const void*)> preprocessingFunc;

In this function I am storing it in a vector that contains structures denoting the type of function and the pointer to the function: 在此函数中,我将其存储在一个向量中,该向量包含表示函数类型和指向函数的指针的结构:

void ImageGraph::addNode(preprocessingFunc func)
{
    NodeFunction nodeFunction = { typeid(func), (void*)func };
    //nodes.push_back(nodeFunction);
}

The error I get: 我得到的错误:

ImagePipeline.cpp: In member function 'void IP::ImageGraph::addNode(IP::preprocessingFunc)': ImagePipeline.cpp:32: error: invalid cast from type 'IP::preprocessingFunc' to type 'void*' /usr/include/c++/4.2.1/typeinfo:135: error: 'std::type_info::type_info(const std::type_info&)' is private ImagePipeline.cpp:32: error: within this context ImagePipeline.cpp:在成员函数'void IP :: ImageGraph :: addNode(IP :: preprocessingFunc)':ImagePipeline.cpp:32:错误:从类型'IP :: preprocessingFunc'强制转换为类型'void *'/ usr /include/c++/4.2.1/typeinfo:135:错误:'std :: type_info :: type_info(const std :: type_info&)'是私有ImagePipeline.cpp:32:错误:在此上下文中

Is it possible to cast this function type to a pointer (void*)? 是否可以将此函数类型转换为指针(void *)? If not, is there a better way to achieve what I am doing? 如果没有,是否有更好的方法来实现我的目标?

For clarity I am storing function pointers to call at later dates, and their are currently 3 types of function pointers, however I would like to store them all in the same vector. 为了清楚起见,我存储了以后要调用的函数指针,它们当前是3种类型的函数指针,但是我想将它们全部存储在同一向量中。

A pointer to a member function can not be stored in a function pointer, because, essentially, a member function has a hidden argument (the "this" pointer). 指向成员函数的指针不能存储在函数指针中,因为从本质上讲,成员函数具有隐藏的参数(“ this”指针)。

There are two solutions: Either a real "pointer to member function" (which requires the correct type of function pointer), or make the function static, and pass in the object itself as a pointer (eg a void). 有两种解决方案:要么是一个真正的“成员函数指针”(需要正确的函数指针类型),要么使函数静态化,然后将对象本身作为指针传递(例如void)。

To me, this seems like kind of an odd solution, and I would think there are better solutions, such as using an interface class, and add your interface objects as "nodes", perhaps? 对我来说,这似乎是一种奇怪的解决方案,我想可能还有更好的解决方案,例如使用接口类,然后将接口对象添加为“节点”?

For a bit more on "pointer to member function", have a look here: http://www.parashift.com/c++-faq/fnptr-vs-memfnptr-types.html 有关“指向成员函数的指针”的更多信息,请在此处查看: http : //www.parashift.com/c++-faq/fnptr-vs-memfnptr-types.html

But given the comment below, you are actually trying to store an object in a pointer. 但是,鉴于以下评论,您实际上是在尝试将对象存储在指针中。 That's similar to (void *)3.1415926; 类似于(void *)3.1415926; - it doesn't make sense. -没有道理。 You probably shouldn't use void * here, but something else, such as pointer to function<> [and change your addNode to take a pointer to a preprocessingFunc object]. 您可能不应该在这里使用void *,而是其他一些东西,例如指向function <>的指针[并更改addNode以采用指向preprocessingFunc对象的指针]。

You're approaching it from the wrong end, it seems. 看来,您是从错误的一端接近它的。 Why are you using void* ? 为什么要使用void* You state that you have " currently 3 types of function pointers". 您声明您有“当前3种类型的函数指针”。 To store callable things, use tr1::function<> . 要存储可调用对象,请使用tr1::function<> It can be initialized from most reasonable sources: function pointers, bound pointers to member functions, functors, and obviously other tr1::function s 可以从最合理的来源进行初始化:函数指针,成员函数的绑定指针,函子以及其他tr1::function

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

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