Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6046999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:18:12+00:00 2026-05-23T07:18:12+00:00

class private_object { private: struct make_public; friend struct make_public; static void method1() {} };

  • 0
class private_object
{
private:
  struct make_public;
  friend struct make_public;
  static void method1() {}
};

struct private_object::make_public
{
  class nested_outer
  {
    void callFromOuter() 
    { private_object::method1(); }   // Should this be an error?

    class nested_inner
    {
      void callFromInner() 
      { private_object::method1(); } // How about this one?
    };
  };
};

This friendship issue came up when I was trying to port an open source project to compile under borland. According to parashift and two semi-related questions here and here, the above example should not be valid.

However, after testing it on seven different compilers1, only borland and dmc complained. This behavior surprised me because I wasn’t expecting friendship to be transitive in nested classes.

So this raises a couple of questions:

  • What is the right behavior? I’m guessing it’s the one accepted by most compilers.
  • If this is the correct behavior, why is this instance of friendship transitivity ok?
  • If this is correct then that would also imply a change in the standard. What might be the reasons for allowing this in the standard?
  • For compilers that rejected this code, what would be an appropriate workaround? Keep in mind that an actual project might contain fairly deep nesting so I’m looking for a solution that’s semi-scalable.

1. tested on mingw-gcc 4.5.2, clang, borland c++ builder2007, digital mars, open watcom, visualc2010 and comeau online

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:18:13+00:00Added an answer on May 23, 2026 at 7:18 am

    In C++03, nested class cannot access private and protected members of enclosing class by default (see §11.8/1). But if you make them friend of the enclosing classs, then they can access them. But again nested class of nested class is still not friend of the outermost enclosing class, the nested class of the nested class cannot access private and protected members of the outermost enclosing class; it cannot even access the private and protected member of the immediate enclosing class, as noted earlier. What you’re doing is that, hence that is not allowed.

    The C++ Standard (2003) says in $11.8/1 [class.access.nest],

    The members of a nested class have no
    special access to members of an
    enclosing class
    , nor to classes or
    functions that have granted friendship
    to an enclosing class; the usual
    access rules (clause 11) shall be
    obeyed. The members of an enclosing
    class have no special access to
    members of a nested class;
    the usual
    access rules (clause 11) shall be
    obeyed.

    Example from the Standard itself:

    class E 
    {
        int x;
        class B { };
        class I 
        {
            B b; // error: E::B is private
            int y;
            void f(E* p, int i)
            {
               p->x = i; // error: E::x is private
            }
       };
       int g(I* p)
       {
           return p->y; // error: I::y is private
       }
    };
    

    Its a defect in the C++03 Standard.

    By the way, its a defect in the C++03 Standard. Since the nested class is a member, it should have access to private and protected members, just like any other member:

    §9.2/1 (C++03):

    Members of a class are data members, member functions (9.3), nested types… Nested types are classes (9.1, 9.7) and enumerations (7.2) defined in the class…

    See this Defect Report :

    • 45. Access to nested classes
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class String { private: char* rep; public: String (const char*); void toUpper() const; };
Consider this: public class TestClass { private String a; private String b; public TestClass()
abstract class Foo { private List<Object> container; private bool update; Foo Foo() { container
Here's my binding source object: Public Class MyListObject Private _mylist As New ObservableCollection(Of String)
by default is a class: private ? internal ? sealed ?
If I have a class as follows class Example_Class { private: int x; int
A little example TTest<T> = class private f : T; public function ToString :
#include iostream class A { private: int a; public : A(): a(-1) {} int
Suppose I have a class public class MyClass { private Set<String> set = new
As an example: public class Foo { private Foo() {} } public class Bar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.