Search code examples
c++oopprotected

C++ cannot access protected member function by friend class


The following example is my demo. At first, I create an abstract class named Expr_node. After that I create another class Int_node. In order to access the private and protected member function normally, I set the Expr class as the friend class of Expr_node. However, I still cannot access to the print function by overload the operator<<.

#include <iostream>
#include <string>
#include <utility>

using namespace std;

class Expr_node;
class Int_node;

class Expr {
  friend ostream& operator<<(ostream&, const Expr&);

  Expr_node* p;

 public:
  Expr(int);
  Expr(const string&, const Expr&);
  Expr(const string&, const Expr&, const Expr&);
  Expr(const Expr& t);
  Expr& operator=(const Expr&);
};

class Expr_node {
  friend ostream& operator<< (ostream&, const Expr_node&);
  friend class Expr;

  int use;
 protected:
  Expr_node(): use(1) {}
  virtual ~Expr_node() = default;
  virtual void print(ostream&) const = 0;
};

ostream& operator<< (ostream& o, const Expr_node& e) {
    e.print(o);
    return o;
}

ostream& operator<< (ostream& o, const Expr& e) {
  e.p->print(o);
  return o;
}

class Int_node: public Expr_node {
  friend class Expr;

  int n;

  explicit Int_node(int k) : n(k) {}
  void print(ostream& o) const override { o << n;}
};

Solution

  • It's the problem of operator<<. It's not class member, but friend function. Implement function inside Expr class, which call print on p. Then call it on Expr parameter. Something like this:

    #include <iostream>
    #include <string>
    #include <utility>
    
    using namespace std;
    
    class Expr_node;
    class Int_node;
    
    class Expr {
      friend ostream& operator<<(ostream&, const Expr&);
    
      Expr_node* p;
    
     public:
      Expr(int);
      Expr(const string&, const Expr&);
      Expr(const string&, const Expr&, const Expr&);
      Expr(const Expr& t);
      Expr& operator=(const Expr&);
    
      void print(ostream& o) const
      {
         this->p->print(o);
      }
    };
    
    class Expr_node {
      friend ostream& operator<< (ostream&, const Expr_node&);
      friend class Expr;
    
      int use;
     protected:
      Expr_node(): use(1) {}
      virtual ~Expr_node() = default;
      virtual void print(ostream&) const = 0;
    };
    
    ostream& operator<< (ostream& o, const Expr_node& e) {
        e.print(o);
        return o;
    }
    
    ostream& operator<< (ostream& o, const Expr& e) {
      e.print(o);
      return o;
    }