gpt4 book ai didi

c++ - C++ 中的循环类型依赖死锁

转载 作者:太空宇宙 更新时间:2023-11-04 15:42:47 25 4
gpt4 key购买 nike

FooBar 成为类型。 Foo 有一个 Bar bar 成员,Bar 有一个 Foo foo 成员:

struct Foo;
struct Bar;

struct Foo { Bar bar; };
struct Bar { Foo foo; };

这不能编译,因为 Foo 必须为要定义的 Bar 定义,但是 Foo 不能定义直到 Bar 已定义 - 死锁。

这个问题有解决办法吗?

最佳答案

万能的标准:

§ 9.2/9

Non-static (9.4) data members shall not have incomplete types. In particular, a class C shall not contain a non-static member of class C, but it can contain a pointer or reference to an object of class C.

和:

§ 9.2/19

If a standard-layout class object has any non-static data members, its address is the same as the address of its first non-static data member. Otherwise, its address is the same as the address of its first base class subobject (if any).

关于c++ - C++ 中的循环类型依赖死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20832868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com