将指针传递给声明为友元的类成员的 pthread
我有一个名为 BaseB 的类,可以从 A 调用它。
我需要让类 A 将指针传递给类 BaseB 中的成员函数的 pthread_create ,该函数也恰好声明为:
friend void ThreadedTask(BaseB* b);
在 public
可访问性下的 >Class BaseB 的 标头。但是,在实际的 cpp 文件中,它并未声明为具有类范围的该类的成员。相反,它被声明为
void ThreadedTask(BaseB* b) { .... }
那么我如何告诉 pthread_create
使用 friended
类成员呢?
我阅读了 第 33 节中的 C++ 常见问题解答 它说:
因为如果没有对象来调用,成员函数就没有意义 打开它,你不能直接执行此操作(如果 X Window 系统是 用 C++ 重写,它可能会传递对周围对象的引用, 不仅仅是指向函数的指针;自然地,这些物体会体现 所需的功能,可能还有更多)。
??
I have a class called BaseB which can be called from A.
I need to have class A pass a pointer to pthread_create of a member function in class BaseB that also happens to be declared as:
friend void ThreadedTask(BaseB* b);
in Class BaseB's header under public
accessibility. However, in the actual cpp file it is not declared as a member of that class with the class scope. Instead, it is declared as
void ThreadedTask(BaseB* b) { .... }
So how can I tell the pthread_create
to use the friended
class member?
I read the C++ FAQ in Section 33 and it says:
Because a member function is meaningless without an object to invoke
it on, you can't do this directly (if The X Window System was
rewritten in C++, it would probably pass references to objects around,
not just pointers to functions; naturally the objects would embody the
required function and probably a whole lot more).
??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解您的情况,您会得到如下内容:
缺少的是
ThreadedTask
的可见声明。要么使声明在 baseb.hpp 中可见,因为它现在实际上是
BaseB
的公共实现细节:或者让
A
自行声明它,这样声明就可以保留“隐”:If I understand your situation correctly, you have something like the following:
What's missing is a visible declaration for
ThreadedTask
.Either make the declaration visible in baseb.hpp, since it's now effectively a public implementation detail of
BaseB
anyway:Or have
A
declare it on its own, so the declaration can remain "hidden":您必须在
BaseB.hpp
中的类声明之外声明ThreadedTask
,或者在A.cpp
中的正确命名空间中声明它。You will either have to declare
ThreadedTask
outside class declaration inBaseB.hpp
, or cheat and declare it in the right namespace inA.cpp
.