C递归头文件包含问题?

发布于 2024-09-24 14:51:13 字数 271 浏览 2 评论 0原文

假设您必须在 2 个头文件中定义相关结构,如下所示:

ah content:

#include b.h

typedef struct A
{
  B *b;
} A;

bh content:

#include a.h

typedef struct B
{
  A *a;
} B;

在这种情况下,这种递归包含是一个问题,但 2 个结构必须指向其他结构,如何实现这一点?

Suppose you have to related structures defined in 2 header files like below:

a.h contents:

#include b.h

typedef struct A
{
  B *b;
} A;

b.h contents:

#include a.h

typedef struct B
{
  A *a;
} B;

In such this case, this recursive inclusion is a problem, but 2 structures must point to other structure, how to accomplish this?

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

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

发布评论

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

评论(4

薄荷梦 2024-10-01 14:51:13

不要 #include ah 和 bh,只需前向声明 A 和 B。

ah:

struct B; //forward declaration
typedef struct A
{
    struct B * b;
} A;

bh:

struct A; //forward declaration
typedef struct B
{
    struct A * a;
} B;

您可能需要考虑这些类的耦合程度。如果它们耦合得非常紧密,那么它们可能属于同一个标头。

注意:您需要在 .c 文件中#include ah 和 bh 来执行 a->b->a 等操作代码>.

Don't #include a.h and b.h, just forward-declare A and B.

a.h:

struct B; //forward declaration
typedef struct A
{
    struct B * b;
} A;

b.h:

struct A; //forward declaration
typedef struct B
{
    struct A * a;
} B;

You might want to think about how tightly coupled the classes are. If they're very tightly coupled, then maybe they belong in the same header.

Note: you'll need to #include both a.h and b.h in the .c files to do things like a->b->a.

尹雨沫 2024-10-01 14:51:13

Google C/C++ 指南建议

当前向声明就足够时,不要使用#include

这意味着:

ahcontents:

typedef struct B B;

typedef struct A
{
  B *b;
} A;

bhcontents:

typedef struct A A;

typedef struct B
{
  A *a;
} B;

如果您更喜欢更安全的东西(但编译时间更长),您可以这样做:

ahcontents:

#pragma once
typedef struct A A;

#include "B.h"

typedef struct A
{
  B *b;
} A;

bhcontents:

#pragma once
typedef struct B B;

#include "A.h"

typedef struct B
{
  A *a;
} B;

Google C/C++ guidelines suggests:

Don't use an #include when a forward declaration would suffice

That'd mean:

a.h contents:

typedef struct B B;

typedef struct A
{
  B *b;
} A;

b.h contents:

typedef struct A A;

typedef struct B
{
  A *a;
} B;

If you prefer something a bit safer (but longer to compile) you can do this:

a.h contents:

#pragma once
typedef struct A A;

#include "B.h"

typedef struct A
{
  B *b;
} A;

b.h contents:

#pragma once
typedef struct B B;

#include "A.h"

typedef struct B
{
  A *a;
} B;
无悔心 2024-10-01 14:51:13

您仅预先定义结构,这样您仍然可以声明指针:

ah 中:

typedef struct B_ B;

typedef struct A_
{
  B *b;
} A;

请注意我如何为 typedef 和 struct 标记使用单独的名称,以使有点清楚了。

You pre-define the struct only, in that way you can still declare a pointer:

In a.h:

typedef struct B_ B;

typedef struct A_
{
  B *b;
} A;

Note how I use separate names for the typedef and struct tags, to make it a bit clearer.

何止钟意 2024-10-01 14:51:13

这会将其剪切为 C:

typedef struct B B;
typedef struct A A;
struct A { B *b; };
struct B { A *a; };

您可以根据需要重新排列 BA

This will cut it in C:

typedef struct B B;
typedef struct A A;
struct A { B *b; };
struct B { A *a; };

You can rearrange B and A as desired.

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