Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    Class Outer that contains class Inner - friends of each other.Lecture 4 - slide 10 : 24
Program 2
// The starting point reproduced from page 851-852 of "The C++ Programming Language", 3ed version.
// With mutual friendship between Outer and Inner classes.
// Solves visibility problems from earlier version of the program.  Compiles.

#include <iostream>
#include <string>

class Outer{
  friend class Inner;                       // Inner is a friend of Outer
private:
  typedef int T;
  int i;
public:
  int i2;                                   
  static int s;

  class Inner{
    friend class Outer;                     // ... and Outer is a friend of Inner
  private:
    int x;
    T y;
  public:
    void fi(Outer *p, int v);
  };

  int fo(Inner* p);
};

void Outer::Inner::fi(Outer *op, int v){
  op->i = v;                                // Now i is visible, for sure
  op->i2 = v;                               //    - because Inner is a friend of Outer
}

int Outer::fo(Inner* ip){
  ip->fi(this,2);
  return ip->x;                             // Now x in Inner is visible
}                                           //    - because Outer is a friend of Inner
                                            
int main(){
  Outer o;  
  Outer::Inner i;

  i.fi(&o, 5);
}