将指针传递给声明为友元的类成员的 pthread

发布于 2024-12-10 06:49:33 字数 815 浏览 1 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-12-17 06:49:33

如果我正确理解您的情况,您会得到如下内容:

/*** baseb.hpp ***/
class BaseB {
    friend void ThreadedTask(BaseB* b);

public:
    // ...
};

/*** baseb.cpp ***/
#include "baseb.hpp"
void ThreadedTask(BaseB* b) {
    // impl
}
// other BaseB definitions

/*** a.hpp ***/
struct A {
    void somefunc();
};

/*** a.cpp ***/
#include "a.hpp"
#include "baseb.hpp"
struct A {
    void somefunc() {
        // pass ThreadedTask to pthread_create
        // but where is ThreadedTask declared?
    }
};

缺少的是 ThreadedTask 的可见声明。

要么使声明在 baseb.hpp 中可见,因为它现在实际上是 BaseB 的公共实现细节:

/*** baseb.hpp ***/
class BaseB;
void ThreadedTask(BaseB* b);

class BaseB {
    friend void ThreadedTask(BaseB* b);

public:
    // ...
};

或者让 A 自行声明它,这样声明就可以保留“隐”:

/*** a.cpp ***/
#include "a.hpp"
#include "baseb.hpp"

void ThreadedTask(BaseB* b);

struct A {
    void somefunc() {
        // pass ThreadedTask to pthread_create
    }
};

If I understand your situation correctly, you have something like the following:

/*** baseb.hpp ***/
class BaseB {
    friend void ThreadedTask(BaseB* b);

public:
    // ...
};

/*** baseb.cpp ***/
#include "baseb.hpp"
void ThreadedTask(BaseB* b) {
    // impl
}
// other BaseB definitions

/*** a.hpp ***/
struct A {
    void somefunc();
};

/*** a.cpp ***/
#include "a.hpp"
#include "baseb.hpp"
struct A {
    void somefunc() {
        // pass ThreadedTask to pthread_create
        // but where is ThreadedTask declared?
    }
};

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:

/*** baseb.hpp ***/
class BaseB;
void ThreadedTask(BaseB* b);

class BaseB {
    friend void ThreadedTask(BaseB* b);

public:
    // ...
};

Or have A declare it on its own, so the declaration can remain "hidden":

/*** a.cpp ***/
#include "a.hpp"
#include "baseb.hpp"

void ThreadedTask(BaseB* b);

struct A {
    void somefunc() {
        // pass ThreadedTask to pthread_create
    }
};
爱已欠费 2024-12-17 06:49:33

您必须在 BaseB.hpp 中的类声明之外声明 ThreadedTask,或者在 A.cpp 中的正确命名空间中声明它。

You will either have to declare ThreadedTask outside class declaration in BaseB.hpp, or cheat and declare it in the right namespace in A.cpp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文