Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

Decent Dabbler

Considering this class:

class Matchable
{
  protected:
    class Match {
      friend class Matchable;

      void append( const Match& match ) {}
    };

  public:
    virtual bool match( const Source& source ) = 0;
};

... where the outer class Matchable is a friend of the inner class Match, and considering this class:

class Literal : public Matchable {

  bool match( const Source& source ) override {
    Matchable::Match m;
    Matchable::Match cm;

    m.append( cm );

    return true;
  }

}

... where Literal is derived from Matchable, I seem to be able to instantiate Matchable::Match in Literal::match() without a problem, yet I am unable to call the private method Matchable::Match::append(), where I expected Literal to inherit the "friendliness" of Matchable.

Is this expected behavior and if so, is there a way to make Literal access private methods of its parent inner class Match?

songyuanyao

Yes, this is expected behavior. See friend declaration

Friendship is not inherited (your friend's children are not your friends)

You might provide a delegate method in Matchable:

class Matchable
{
  protected:
    class Match {
      friend class Matchable;
      void append( const Match& match ) {}
    };
    void appendMatch( Match& match, const Match& matched ) { 
        match.append(matched);
    }

  public:
    virtual bool match( const Source& source ) = 0;
};

then

class Literal : public Matchable {

  bool match( const Source& source ) override {
    Matchable::Match m;
    Matchable::Match cm;

    appendMatch(m, cm);

    return true;
  }

}

Otherwise you might make Match::append public (which make friend declaration meaningless).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

Friend function of a private inner class

From Dev

Friend function of a private inner class

From Dev

Derived class can't access protected method of base class

From Dev

Inner class method and private fields

From Dev

Inner private class and public method

From Dev

Is there any way to access a private inner class that is nested in a super class from a derived class in Java?

From Dev

Java cannot access a protected variable in inner class

From Dev

Instantiate an inner class inherited with protected access specifier

From Dev

abstract private inner class

From Dev

abstract private inner class

From Dev

Inner Class method access to parent method's variable

From Dev

Protected method access from derived class

From Dev

Calling private method inside private class inside Inner class

From Dev

Inner class, pimpl and a friend class - disagreeing compilers

From Dev

How do I access an inner class constructor from a derived class?

From Dev

Make protected inner class public

From Dev

How to Access private parameterized method of private inner class with in a static class in JAVA

From Dev

Method local inner class vs inner class

From Dev

inner class have access to private final methods in base class but why?

From Dev

How a friend class can access a private member of a nested class?

From Dev

C++: How can I access a method of an inner class?

From Dev

Scope of private variables in an inner class?

From Dev

Private static inner class - Powermock

From Dev

Visibility of a private inner class of a public class

From Dev

How can I access an inner class?

From Dev

Can I access the "secondary this" from an inner class?

From Java

Access method of outer anonymous class from inner anonymous class

From Dev

Access method of outer anonymous class from inner anonymous class

Related Related

  1. 1

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  2. 2

    Friend function of a private inner class

  3. 3

    Friend function of a private inner class

  4. 4

    Derived class can't access protected method of base class

  5. 5

    Inner class method and private fields

  6. 6

    Inner private class and public method

  7. 7

    Is there any way to access a private inner class that is nested in a super class from a derived class in Java?

  8. 8

    Java cannot access a protected variable in inner class

  9. 9

    Instantiate an inner class inherited with protected access specifier

  10. 10

    abstract private inner class

  11. 11

    abstract private inner class

  12. 12

    Inner Class method access to parent method's variable

  13. 13

    Protected method access from derived class

  14. 14

    Calling private method inside private class inside Inner class

  15. 15

    Inner class, pimpl and a friend class - disagreeing compilers

  16. 16

    How do I access an inner class constructor from a derived class?

  17. 17

    Make protected inner class public

  18. 18

    How to Access private parameterized method of private inner class with in a static class in JAVA

  19. 19

    Method local inner class vs inner class

  20. 20

    inner class have access to private final methods in base class but why?

  21. 21

    How a friend class can access a private member of a nested class?

  22. 22

    C++: How can I access a method of an inner class?

  23. 23

    Scope of private variables in an inner class?

  24. 24

    Private static inner class - Powermock

  25. 25

    Visibility of a private inner class of a public class

  26. 26

    How can I access an inner class?

  27. 27

    Can I access the "secondary this" from an inner class?

  28. 28

    Access method of outer anonymous class from inner anonymous class

  29. 29

    Access method of outer anonymous class from inner anonymous class

HotTag

Archive