从共享对象调用外部类成员
我有两个类 Fifo:
#ifdef __cplusplus
extern "C" {
#endif
class Fifo
{
public:
Fifo(int len);
~Fifo();
void AddTokens(void* buffer, unsigned len);
private:
// some variables here
};
#ifdef __cplusplus
}
#endif
和另一个类 Process_Wrapper:
#ifdef __cplusplus
extern "C" {
#endif
class Process_Wrapper
{
public:
Process_Wrapper(const std::string process_name);
Fifo* GetOutputPortIDtoFifoMap(int portID);
virtual ~Process_Wrapper();
protected:
private:
//some variables here
};
#ifdef __cplusplus
}
#endif
成员函数 Process_Wrapper::GetOutputPortIDtoFifoMap 返回一个指向 Fifo 类的已初始化对象的指针。
现在,我有一个共享对象 (.so),它是使用包含类 Fifo
和 Process_Wrapper
的标头进行编译的。也就是说,.so 文件知道这些类的接口。
我从 Process_Wrapper 的成员函数 (Process_Wrapper::function2
) 调用此 .so 文件,并向 .so 传递一个指针 - this
。因此,我假设共享对象将了解有关 Process_Wrapper 类的所有信息。
在.so文件中,我尝试调用成员函数Process_Wrapper::GetOutputPortIDtoFifoMap
,这显然不起作用。所以,我尝试了:
Fifo* (Process_Wrapper::*GetFifoMapping)(int) = p1->wptr->GetOutputPortIDtoFifoMap;
其中 p1->wptr
正确返回指向 Process_Wrapper
对象的指针。
我在这里迷路了。我对这个概念很陌生,这些例子并不能直接回答问题。我该如何去做呢?
另外,由于我使用的是 C++0x/C++11,任何人都可以告诉我(最好)使用 std::bind 或 mem__fun_ref 的解决方案吗?
编辑:好的,我想我可以完善我的问题:我有一个类Myclass
,和一个对象myobject
,它是这个的一个实例班级。 Myclass
有一个成员函数,它接受一些参数,并返回一个 PTRTYPE
类型的指针。如何提取 Myclass 的成员函数以传递给另一个函数,以便它可以用作普通的函数指针。或者,具体来说:如何从指向成员函数的绑定指针中提取函数指针?
After lot of reading here and here, I still cannot get my code to work. Here is the problem:
I have two classes, Fifo:
#ifdef __cplusplus
extern "C" {
#endif
class Fifo
{
public:
Fifo(int len);
~Fifo();
void AddTokens(void* buffer, unsigned len);
private:
// some variables here
};
#ifdef __cplusplus
}
#endif
and another class Process_Wrapper:
#ifdef __cplusplus
extern "C" {
#endif
class Process_Wrapper
{
public:
Process_Wrapper(const std::string process_name);
Fifo* GetOutputPortIDtoFifoMap(int portID);
virtual ~Process_Wrapper();
protected:
private:
//some variables here
};
#ifdef __cplusplus
}
#endif
The member function Process_Wrapper::GetOutputPortIDtoFifoMap
returns a pointer to an initialized object of class Fifo
.
Now, I have a shared object (.so), which is compiled with headers for classes Fifo
and Process_Wrapper
included. That is, the .so file knows the interfaces to these classes.
I call this .so file from a member function of Process_Wrapper
(Process_Wrapper::function2
) and pass to .so a pointer - this
. So, I assume that the shared object will know everything about the class Process_Wrapper
.
In the .so file, I am trying to call the member function Process_Wrapper::GetOutputPortIDtoFifoMap
, which obviously does not work. So, I tried:
Fifo* (Process_Wrapper::*GetFifoMapping)(int) = p1->wptr->GetOutputPortIDtoFifoMap;
where p1->wptr
returns a pointer to Process_Wrapper
object correctly.
I am lost here. I am quite new to this concept, and the examples do not directly answer the question. How do I go about doing it?
Also, since I am using C++0x/C++11, can anyone tell me (preferably) the solution using std::bind, or mem__fun_ref?
Edit: Okay, I think I can refine my question: I have a class Myclass
, and an object myobject
, which is an instance of this class. Myclass
has a member function, taking some arguments, and returning a pointer of type PTRTYPE
. How do I extract the member function of Myclass
to be passed to another function, such that it can be used as a normal pointer-to-function. Or, specifically: How do I extracting the function pointer from a bound pointer to member function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了回答我自己的问题,我将“this”指针传递给 .so 对象。但是,在运行时,我收到有关未找到符号的错误。该符号在 .so 文件中为“U”或未映射,但在加载该 .so 的主 exe (“T”) 中定义。我所要做的就是将“-rdynamic”添加到生成主 exe 的链接器选项中。
显然,我没有看到问题那么简单,我的(错误的)假设是问题与正确使用指向已初始化对象的成员函数的指针有关。
我知道我发布的问题描述可能不太清楚,所以对此表示歉意。
谢谢大家。
To answer my own question, I was passing "this" pointer to the .so object. However, at run-time, I was getting the error about a symbol not found. This symbol was 'U' or unmapped in the .so file but was defined in the main exe ('T') which would load this .so. All I had to do was add '-rdynamic' to the linker option that produced the main exe.
Clearly, I did not see the problem was that simple, and my (incorrect) hypothesis was that the problem was related to correctly using the pointer to member function of an initialized object.
I understand that the problem description I posted may not have been clear, so apologies for that.
Thanks everyone.