非公共类型的公共别名


12

我想知道它是否有效的C ++:

class Test {
    struct PrivateInner {
        PrivateInner(std::string const &str) {
            std::cout << str << "\n";
        }
    };

public:
    using PublicInner = PrivateInner;
};

//Test::PrivateInner priv("Hello world");        // Ok, private so we can't use that
Test::PublicInner publ("Hello World");           // ?, by using public alias we can access private type, is it ok ?

1
为什么代码无效?
NathanOliver

Answers:


14

类型既不是公共的也不是私有的。访问控制仅适用于名称。由于PublicInner是引用该类的公共名称PrivateInner,因此可以在Test该类之外使用。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.