Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public alias for non-public type

I wonder if it is valid 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 ?
like image 781
Johnmph Avatar asked Nov 22 '19 17:11

Johnmph


1 Answers

Types are neither public nor private. Access control only ever applies to names. Since PublicInner is a public name that refers to the PrivateInner class, it can be used outside the Test class.

like image 187
Brian Bi Avatar answered Oct 25 '22 09:10

Brian Bi