| Class Outer that contains class Inner - does not compile. | Lecture 4 - slide 10 : 24 Program 1 |
// Reproduced from page 851-852 of "The C++ Programming Language", 3ed version.
// Intended to show that instances of an inner class does not have access to private members
// in instances of an outer class (as described in "The C++ Programming Language").
// This cannot be reproduced with use of g++.
// Does not compile.
#include <iostream>
#include <string>
class Outer{
private:
typedef int T; // Type T and int i are private in Outer
int i;
public:
int i2;
static int s;
class Inner{
private:
int x;
T y; // Expected error: Outer::T is private. g++ can use private type in surrounding class.
public:
void fi(Outer *op, int v);
};
int fo(Inner* ip);
};
void Outer::Inner::fi(Outer *op, int v){
op->i = v; // Expected error: Outer::i is private. g++ can use private variable in surrounding class.
op->i2 = v; // OK - i2 is public
}
int Outer::fo(Inner* ip){
ip->fi(this,2); // OK - Inner::fi is public
return ip->x; // error: Inner::x is private. Even g++ cannot use a private variable in inner class.
}
int main(){
Outer o;
Outer::Inner i;
i.fi(&o, 5);
o.fo(&i);
}