class 与 C++ 中 class 中的成员相同吗?

Same class as a member inside a class in C++?

抱歉,我的问题构思不当earlier。这段代码是这样的:

class Bar
{
    public:
        // some stuff

    private:
        struct Foo
        {
            std::unordered_map<std::string, std::unique_ptr<Foo>> subFoo;
            // some other basic variables here
        };

        Foo foo;
};

我对 subFoo 有了基本的了解。但是我想知道 Bar 的单个实例将只包含 Foo 的单个实例,即 foo 成员变量?所以单个 instance/object 的 Bar 将无法在 subFoo?

中映射多个 Foo

感觉这里少了什么,谁能帮我分解一下?

关于嵌套 class 定义的误解多于实际好处。在您的代码中,它真的无关紧要,我们可以将其更改为:

struct Foo {
    std::unordered_map<std::string, std::unique_ptr<Foo>> subFoo;
    // some other basic variables here
};

class Bar
{
        Foo foo;
};

Foo 现在定义在不同的范围内,不再是 privateBar。否则对当前代码没有影响。

I am wondering that a single instance of Bar will contain only a single instance of Foo that is foo member variable?

是的。

So a single instance/object of Bar will not be able to map multiple Foo inside the subFoo?

subFoo 是一个包含指向 Foo 的唯一指针的映射。 Bar::foo 不是由唯一指针管理的,因此将它们放在 subFoo 中是不可能的,如果没有 运行 进入双重释放错误。 std::unique_ptr 可以与自定义删除器一起使用,但这里不是这种情况。因此,您不能在任何 Foo::subFoo 中存储指向 Bar::foo 的唯一指针。但是,您可以在 Foo::subFoo.

中存储指向其他 Foo 的唯一指针