c++ access protected member via auto

时间:2018-02-03 07:55:33

标签: c++ protected

Access of a protected inner class via "auto". Using MSVC 14. Using the class name directly, I can't access the inner class yet using auto it works. Why?

class Car
{
    class Driver;
public:
    static Driver* createDriver()
    {
        return new Driver;
    }

protected:
    class Driver
    {
    public:
        std::string name;
    };
};

void test()
{
    //these two lines fail to compile as Car::Driver is protected.
    //Car::Driver* driver = Car::createDriver();
    //driver->name = "John";

    //but why does this work?
    auto driver2 = Car::createDriver();
    driver2->name = "John";

}

0 个答案:

没有答案