{"id":757,"date":"2020-01-31T14:27:43","date_gmt":"2020-01-31T22:27:43","guid":{"rendered":"http:\/\/209.126.2.187\/?p=757"},"modified":"2020-01-31T14:27:43","modified_gmt":"2020-01-31T22:27:43","slug":"scoping-and-friends-in-nested-classes","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2020\/01\/31\/scoping-and-friends-in-nested-classes\/","title":{"rendered":"Scoping and Friends in Nested Classes"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce the scoping in nested classes and how friend classes work here.<\/p>\n<h2>Conclusion<\/h2>\n<p>The rules are,<\/p>\n<ol>\n<li>As any member of its outer class, nested classes have access to all names (private, protected, etc) to which the outer class has access; however, outer classes have no special access to inner classes&#8217; members;<\/li>\n<li>Declarations in a nested class can use any members of the enclosing class, following the usual usage rules for the non-static members;<\/li>\n<li>B wants to use the private functions in A. Declare the <code>friend class B<\/code> in A, which means B is a friend of A.<\/li>\n<\/ol>\n<pre><code class=\"language-c++\">class PrivateClass;\n\nclass Outer {\nprivate:\n    static void PrivateFunction() {}\n\npublic:\n    class Inner {\n        \/\/ outer classes do not have special access to private members of inner classes\n        \/\/  to call Inner::PrivateFunction(), Outer must be a friend of Inner\n        friend class Outer;\n    public:\n        Inner();\n\n    private:\n        static void PrivateFunction() {}\n    };\npublic:\n    void PublicFunction() {\n        \/\/ outer classes do not have special access to private members of inner classes\n        Inner::PrivateFunction();\n    }\n};\n\nclass PrivateClass {\n    \/\/ nested classes can be a friend of other normal classes\n    friend class Outer::Inner;\nprivate:\n    explicit PrivateClass(int a) {}\n};\n\nOuter::Inner::Inner() {\n    PrivateClass instance(1);\n    \/\/ nested classes can access private members of outer classes\n    PrivateFunction(); \n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce the scoping in nested classes and how friend classes work here. Conclusion The rules are, As any member of its outer class, nested classes have access to all names (private, protected, etc) to which the outer class has access; however, outer classes have no special access to inner&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-757","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=757"}],"version-history":[{"count":1,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/757\/revisions"}],"predecessor-version":[{"id":758,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/757\/revisions\/758"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}