Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Outer class access Inner class's private - why forbidden

Tags:

c++

private

Hello I am wondering why C++ standard allows us in nested classes to access outer class's private fields, while it forbids to access inner class's private fields from the outer class. I understand, that this example:

class OuterClass{
public:
    class InnerClass{
    public:
        void printOuterClass(OuterClass& outer) {cout << outer.m_dataToDisplay;};
    };
private:
    int m_dataToDisplay;
};

is fine, because thing, that Inner class sometimes can be complicated. But I think following scenario is also fine:

class Algorithm{
public:
    class AlgorithmResults{
    public:
        void readAlgorithmResult();
    private:
        void writeAlgorithmResult();
    };

    void calculate(AlgorithmResults& results, Arguments...){
       //calculate stuff
       results.writeAlgorithmResult(results);
    }
};

For me this structure makes perfect sense, although it is not allowed in C++. I also noticed, that for some time both were allowed in Java, but now second example is also forbidden. What is the reason, that first example is allowed and another is denied?

like image 823
DawidPi Avatar asked Mar 15 '16 10:03

DawidPi


People also ask

Can outer class access inner class private?

If the inner class defined as private and protected, can outer class access the members of inner class? Yes.

Can outer class access inner class private members C++?

Accessing members of an outer class inside nested class since c++11. "Declarations in a nested class can use any members of the enclosing class, following the usual usage rules for the non-static members."

Can inner class access outer class private variables C#?

The inner class can access any non-static member that has been declared in the outer class. Scope of a nested class is limited by the scope of its (outer) enclosing class. If nothing is specified, the nested class is private (default). Any class can be inherited into another class in C# (including a nested class).

Is Outer class Cannot be private but an inner class can be private?

Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class. Following is the program to create an inner class and access it.


2 Answers

Essentially, within a scope names declared earlier in that scope are valid and can be used directly (unless they're shadowed). Code outside a scope can't directly use names declared inside the scope. E.g. code after a curly braces block, can't directly use variables declared inside that block (an example of indirect use is when the outside code has access to a pointer to a static variable inside the curly braces block).


For the second example, just make Algorithm a friend of AlgorithmResults:

class AlgorithmResults
{
    friend class Algorithm;
like image 115
Cheers and hth. - Alf Avatar answered Sep 21 '22 08:09

Cheers and hth. - Alf


The nested classes could access outer class's private fields, because it's a member of the outer class, just same as the other members.

[class.access.nest]/1

A nested class is a member and as such has the same access rights as any other member.

On the other hand, the outer class doesn't have special access rights on the nested class, they're just normal relationship.

The members of an enclosing class have no special access to members of a nested class; the usual access rules ([class.access]) shall be obeyed. [ Example:

class E {
  int x;
  class B { };

  class I {
    B b;                        // OK: E​::​I can access E​::​B
    int y;
    void f(E* p, int i) {
      p->x = i;                 // OK: E​::​I can access E​::​x
    }
  };

  int g(I* p) {
    return p->y;                // error: I​::​y is private
  }
};

— end example ]

like image 30
songyuanyao Avatar answered Sep 19 '22 08:09

songyuanyao