友元类互相指向对方

发布于 2024-10-08 02:34:25 字数 230 浏览 8 评论 0原文

如何创建两个类,它们具有指向彼此类类型的成员指针,并且可以完全访问彼此的数据?换句话说,我需要两个这样的类:

class A
{
protected:
   B *p;
};

class B
{
protected:
   A *p;
};

我遇到了麻烦,因为我不符合 C++ 约定,显然,类 A 不能声明类 B,因为 B 是稍后在代码中声明的。

感谢您的帮助

How can I create two classes that have member pointers to each other's class type, with full access to each other's data? In other words, I need two classes like this:

class A
{
protected:
   B *p;
};

class B
{
protected:
   A *p;
};

I'm having trouble with it because I'm not up to par with C++ conventions, and obviously, class A can't declare a class B because B is declared later in the code.

Thanks for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

遥远的绿洲 2024-10-15 02:34:25

您应该使用前向类声明。

//in A.h

    class B; // forward declaration
    class A
    {
    protected:
       B *p;
       friend class B; // forward declaration
    };

//in B.h
class A; // forward declaration
class B
{
protected:
   A *p;
   friend class A; // forward declaration
};

You should use forward class declaration.

//in A.h

    class B; // forward declaration
    class A
    {
    protected:
       B *p;
       friend class B; // forward declaration
    };

//in B.h
class A; // forward declaration
class B
{
protected:
   A *p;
   friend class A; // forward declaration
};
相思故 2024-10-15 02:34:25
class B;
class A {
    friend class B;
  protected:
    B *p;
};

class B {
    friend class A;
  protected:
    A *p;
};

请注意,实际使用 B 成员的任何 A 成员函数都必须在 B 定义之后定义,例如:

class B;
class A {
    friend class B;
  protected:
    B *p;
    A *getBA();
};

class B {
    friend class A;
  protected:
    A *p;
};

A *A::getBA() { return p->p; }
class B;
class A {
    friend class B;
  protected:
    B *p;
};

class B {
    friend class A;
  protected:
    A *p;
};

Note that any member functions of A which actually use the members of B will have to be defined after the definition of B, for example:

class B;
class A {
    friend class B;
  protected:
    B *p;
    A *getBA();
};

class B {
    friend class A;
  protected:
    A *p;
};

A *A::getBA() { return p->p; }
甜嗑 2024-10-15 02:34:25

您必须使用前向声明,例如:

class B;
class A{
   ...
   friend class B;
};

you must use forward declaration like:

class B;
class A{
   ...
   friend class B;
};
旧情别恋 2024-10-15 02:34:25
class A
{
protected:
   class B *p;
};

如果你想声明友谊,你需要一个前置声明:

class B;

class A
{
friend class B;
protected:
   B *p;
};
class A
{
protected:
   class B *p;
};

If you want to declare friendship, you need a forward declaration:

class B;

class A
{
friend class B;
protected:
   B *p;
};
南街九尾狐 2024-10-15 02:34:25
class B;
class A
{
protected:
   B *p;
   friend class B;
};

class B
{
protected:
   A *p;
   friend class A;
};
class B;
class A
{
protected:
   B *p;
   friend class B;
};

class B
{
protected:
   A *p;
   friend class A;
};
小梨窩很甜 2024-10-15 02:34:25

您可以通过在 A 类之上执行 class B; 来使用前向声明

You could use a forward declaration by doing class B; above class A

如歌彻婉言 2024-10-15 02:34:25

简单使用:
B 级;

class A
{
    protected:
       B *p;
       friend class B;
};

class B
{
    protected:
       A *p;
       friend class A;
};

使用B类;意味着前向声明,这基本上告诉编译器:“B 类存在于某处”。

simple use:
class B;

class A
{
    protected:
       B *p;
       friend class B;
};

class B
{
    protected:
       A *p;
       friend class A;
};

Using class B; means a forward declaration and this basically tells the compiler: "class B exists somewhere".

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