两个类可以使用 C++ 互相查看吗?

发布于 2024-08-12 20:14:49 字数 95 浏览 7 评论 0原文

所以我有一个 A 类,我想在其中调用一些 B 类函数。所以我包括“bh”。但是,在 B 类中,我想调用 A 类函数。如果我包含“啊”,它最终会陷入无限循环,对吗?我能做什么呢?

So I have a class A, where I want to call some class B functions. So I include "b.h". But, in class B, I want to call a class A function. If I include "a.h", it ends up in an infinite loop, right? What can I do about it?

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

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

发布评论

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

评论(4

愛上了 2024-08-19 20:14:49

仅将成员函数声明放在头文件 (.h) 中,并将成员函数定义放在实现文件 (.cpp) 中。那么您的头文件不需要相互包含,并且您可以在任一实现文件中包含这两个头文件。

如果您还需要在成员签名中引用其他类,则可以使用前向声明:

class A;

这使您可以使用指针和引用类型(A*A& >),尽管不是 A 本身。它也不让你打电话给会员。

例子:

// a.h
struct B; // forward declaration

struct A {
   void foo(B* b); // pointers and references to forward-declared classes are ok
};


// b.h
struct A; // forward declaration

struct B {
   void bar(A& a); // pointers and references to forward-declared classes are ok
};


// a.cpp
#include "a.h"
#include "b.h"

void A::foo(B* b) {
   b->bar(*this); // full declaration of B visible, ok to call members now
}


// b.cpp
#include "a.h"
#include "b.h"

void B::bar(A& a) {
   a.foo(this); // full declaration of A visible, ok to call members now
}

Put only member function declarations in header (.h) files, and put member function definitions in implementation (.cpp) files. Then your header files do not need to include each other, and you can include both headers in either implementation file.

For cases when you need to reference the other class in member signatures as well, you can use a forward declaration:

class A;

This lets you use pointer and reference types (A* and A&), though not A itself. It also doesn't let you call members.

Example:

// a.h
struct B; // forward declaration

struct A {
   void foo(B* b); // pointers and references to forward-declared classes are ok
};


// b.h
struct A; // forward declaration

struct B {
   void bar(A& a); // pointers and references to forward-declared classes are ok
};


// a.cpp
#include "a.h"
#include "b.h"

void A::foo(B* b) {
   b->bar(*this); // full declaration of B visible, ok to call members now
}


// b.cpp
#include "a.h"
#include "b.h"

void B::bar(A& a) {
   a.foo(this); // full declaration of A visible, ok to call members now
}
£烟消云散 2024-08-19 20:14:49

每个类(A 和 B)应该有一个头文件和一个实现文件。

每个头文件(例如 Ah)不应包含其他头文件(例如 Bh),但可以包含对其他类的前向引用(例如类似 的语句) class B;),然后可以在其声明中使用对另一个类的指针和/或引用(例如,class A 可以包含 B* 作为数据成员和/或作为方法参数)。

每个CPP 文件(例如A.cpp)可以包含多个头文件(例如AhBh)。建议每个 CPP 文件首先包含自己的头文件(例如 A.cpp 应包含 Ah,然后包含 Bh,而 B.cpp 应包含 Bh,然后包含 Ah)。

每个头文件应该只包含声明,而不是类的定义:例如,它将列出类方法的签名,但不列出方法体/实现(方法体/实现将位于 .cpp 文件,不在头文件中)。由于头文件不包含实现细节,因此它们不依赖(不需要查看)其他类的细节;他们最多需要知道,例如,B 是一个类的名称:它可以从前向声明中获得,而不是通过在另一个头文件中包含一个头文件。

Each class (A and B) should have a header file and an implementation file.

Each header file (e.g. A.h) should not include the other header file (e.g. B.h) but may include a forward reference to the other class (e.g. a statement like class B;), and may then use pointers and/or references to the other class in its declaration (e.g. class A may contain a B* as a data member and/or as a method parameter).

Each CPP file (e.g. A.cpp) may include more than one header file (e.g. A.h and B.h). It's recommended that each CPP file should include its own header file first (e.g. A.cpp should include A.h and then B.h, whereas B.cpp should include B.h and then A.h).

Each header file should contain only the declaration, and not the definition of the class: for example it will list the signatures of the class' methods, but not the method bodies/implementations (the method bodies/implementations will be in the .cpp file, not in the header file). Because the header files don't contain implemention details, they therefore don't depend on (don't need to see) details of other classes; at most they need to know that, for example, B is the name of a class: which it can get from a forward declaratin, instead of by including a header file in another header file.

月隐月明月朦胧 2024-08-19 20:14:49

您还可以使用前向声明来解决该问题。

You can also use forward declarations to get around the issue.

始于初秋 2024-08-19 20:14:49

尝试将 #ifndef#define#endif 放在 .h 文件周围。

Try putting #ifndef, #define and #endif around your .h files.

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