Access specifiers in nested class

This is probably a really basic question, but I'm confused by why this code compiles

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Foo {
public:

    Foo()
    : value(100) { }
 
private:

    class Bar {
    public:      
        Bar(Foo& foo)
        : foo(foo) { }
        
        void Update()
        {
            foo.value = 42;
        }
        
    private:
        Foo& foo;
    };
            
    int value;
};

int main()
{

}


Specifically, line 16.
Isn't foo.value a private member?
I assume it's something special with it being a nested class?
Last edited on
https://en.cppreference.com/w/cpp/language/nested_types.html
Like any member of its enclosing class, the nested class has access to all names (private, protected, etc) to which the enclosing class has access, but it is otherwise independent and has no special access to the this pointer of the enclosing class.
Thanks Peter.
Registered users can post here. Sign in or register to post.