0

Sorry I ill formed the question earlier. The piece of code is something like:

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;
};

I got the basic idea about subFoo. But 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?

It feels like I am missing something here, can anyone break it down for me?

DonBaka
  • 325
  • 2
  • 14
  • 1
    1. Yes. 2. No, because even though there is a single `subFoo` inside `Foo` inside `Bar`, that `subFoo` is a container (`unordered_map`) which may have arbitrary number of values. – yeputons Nov 25 '21 at 14:08
  • @yeputons can you give an example with two elements inside `subFoo`, it will be clear enough for me, thanks – DonBaka Nov 25 '21 at 14:15

1 Answers1

1

There are more misunderstandings about nested class definitions than there are actual benefits. In your code it really does not matter much and we can change it to:

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

class Bar
{
        Foo foo;
};

Foo is now defined in a different scope and it is no longer private to Bar. Otherwise it makes no difference for the present code.

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

Yes.

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

subFoo is a map holding unique pointers to Foos. Bar::foo is not managed by a unique pointer, hence placing them in subFoo is not possible without running into a double free error. std::unique_ptr can be used with a custom deleter, but thats not the case here. Hence you cannot store a unique pointer to Bar::foo in any Foo::subFoo. You can however, store unique pointers to other Foos in Foo::subFoo.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • just a small question, `Foo` is private to `Bar`, so I am wondering can it access another `Foo` instance from another `Bar`? – DonBaka Nov 25 '21 at 14:30
  • 1
    @DonBaka any `Foo` can access another `Foo`s members. Also note that nested classes defined in the `private` section arent as private as you might expect. For example if `Bar` has a public method with a `Foo` parameter or return type then the type isnt really private – 463035818_is_not_an_ai Nov 25 '21 at 14:32
  • one last question, another `Foo` means another `Bar` as well? (that is number of `Foo` instance == number of `Bar` instance)? – DonBaka Nov 25 '21 at 14:37
  • 1
    @DonBaka franky, thats very basic questions and I think the best I can do is to refer you to a good C++ book https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Nov 25 '21 at 14:50