Why does the auto keyword allow instances of private declared inner classes or inner structures?

I found something strange in C ++ that I don't understand why this is happening.

I have an inner class with a private structure definition, it looks like this:

#include <string>
class A {
private:
    class B {
    private:
        struct C {
            int lol;
            std::string ok;
        };
    public:
        B() {}
        C* makething();
    };
public:
    A() {}
    void dostuff();
};

C* makething(); just returns a new C structure, for example:

A::B::C* A::B::makething()
{
    C* x = new C;
    return x;
} 

So, if the implementation for void dostuff();looks like this:

void A::dostuff()
{
    B x = B();
    B::C* thing = x.makething(); // compile error
    thing->lol = 42;
    thing->ok = "some words";
    std::cout << thing->lol << " " << thing->ok;
}

This gives me an error: C2248 'A::B::C': cannot access private struct declared in class 'A::B'This was expected because struct C is declared as private in class B.

However, if I changed the string B::C* thing = x.makething();to auto thing = x.makething();, it compiles and will no longer give me an error that struct C is private, and then I can change the structure values ​​as it was in the makething()function.

? auto ?

: Visual Studio Community 2015 .

2: , , , . , , .

+4

Source: https://habr.com/ru/post/1618197/


All Articles