3.16 Nested Types

NestedClass.cc

#include <iostream>
using namespace std;

class X {                                                         //(A)

    class Y{                                                      //(B)
        int m;
    public:
        Y( int mm ) { m = mm; }                                   //(C)
        void printY(){ cout << "m of nested class object: " 
                            << m << endl; };                      //(D)
    };

    Y* yptr;                                                      //(E)
public:
    X() { yptr = new Y( 100 ); }                                  //(F)
    Y* get_yptr(){ return yptr; }                                 //(G)
};


int main() {                           
    X x;                                                          //(H)
    x.get_yptr()->printY();                                       //(I)
    return 0;
}

NestedClassDefsNotInLine.cc

#include <iostream>
using namespace std;

class X {                                                         //(A)
    class Y{                                                      //(B)
        int m;
    public:
        Y( int mm );                                              //(C)
        void printY();                                            //(D)
    };

    Y* yptr;                                                      //(E)
public:
    X();                                                          //(F)
    Y* get_yptr();                                                //(G)
};

//Definitions specific to the enclosing class X:
X::X() { yptr = new Y( 100 ); }                                   //(H)
X::Y* X::get_yptr(){ return yptr; }                               //(I)

//Definitions specific to the nested class Y:
X::Y::Y( int mm ) { m = mm; }                                     //(J)
void X::Y::printY(){ cout << "m of nested class object: " 
                          << m << endl; }                         //(K)

int main() {
    X x;
    x.get_yptr()->printY();
    return 0;
}

EnclosingClassAccess.cc

#include <iostream>

class X {                           
public:
    int regularIntEnclosing;                                      //(A)
    static int staticIntEnclosing;                                //(B)

    class Y{                        
    public:
        int m;
        int n;
        Y( X* xptr ) { 
            m = xptr->regularIntEnclosing;                        //(C)
            n = staticIntEnclosing;                               //(D)
        }
    };

    X( int n ) { regularIntEnclosing = n; }                       //(E)  
};

int X::staticIntEnclosing = 300;                                  //(F)

int main() {                           
    X* xptr = new X( 100 );
    X::Y y( xptr );
    return 0;
}

NestedTypes.cc

class X {};

class Y {
public:
    typedef X Z;                                                  //(A)
    enum Weight { light, medium, heavy };                         //(B)
private:
    Z zobj;
    Weight weight;
};

int main() {
    // Z zobj;                      // error
    Y::Z zobj;                      // ok
    // Weight w = medium;           // error
    // Y::Weight w = medium;        // error
    Y::Weight w = Y::medium;        // ok
    return 0;
}

NestedClass.java

class X {                                                         //(A)
    static class Y{                                               //(B)
        private int m;
        public Y( int mm ) { m = mm; }      
        public void printY(){               
            System.out.println( "m of nested class object: " + m );
        }
    }

    private Y yref;                         

    public X() { yref = new Y( 100 ); }     

    Y get_yref(){ return yref; }            
}

class Test {
    public static void main( String[] args ) {
        X x = new X();
        x.get_yref().printY();   // m of nested class object: 100 
    }
}

NestedClassAsType.java

class X {                                                           

    public static class Y{                                        //(A)
        private int m;
        public Y( int mm ) { m = mm; }                              
        public void printY(){                                       
            System.out.println( "m of nested class obj: " + m );
        }
    }

    private Y yref;                                                 

    public X() { yref = new Y( 100 ); }                             

    Y get_yref(){ return yref; }                                    
}


class Test {
    public static void main( String[] args ) {                     
        X x = new X();                                             
        x.get_yref().printY();  // m of nested class obj: 100

        X.Y y = new X.Y( 200 );                                   //(B)
        y.printY();             // m of nested class obj: 200
    }
}

EnclosingClassAccess.java

class X {                           
    private int regularIntEnclosing;                              //(A)
    private static int staticIntEnclosing = 300;                  //(B)

    public static class Y{                        
        private int m;
        private int n;
        Y( X xref ) { 
            m = xref.regularIntEnclosing;                         //(C)
            n = staticIntEnclosing;                               //(D)
        }
    }

    public X( int n ) { regularIntEnclosing = n; }                //(E)    
}

class Test {
    public static void main( String[] args ) {
        X x = new X( 100 );
        X.Y y = new X.Y( x );                                     //(F)
    }
}

InnerClass.java

class X {                           
    private int regularIntEnclosing;                              //(A)
    private static int staticIntEnclosing = 300;                  //(B)

    public class Y{                        
        private int m;
        private int n;
        public Y() { 
            m = regularIntEnclosing;                              //(C)
            n = staticIntEnclosing;                               //(D)
        }
    }

    public X( int n ) { regularIntEnclosing = n; }                //(E) 
}

class Test {
    public static void main( String[] args ) {
        X x = new X( 100 );                                       //(F)
        // X.Y y = new X.Y();             // error
        X.Y y = x.new Y();                // ok                   //(G)
    }
}

InnerClassThisPrefix.java

class X {                           
    private int m;                                                //(A)
    private static int n = 300;                                   //(B)

    public class Y{                        
        private int m;                                            //(C)
        private int n;                                            //(D)
        public Y() { 
            this.m = X.this.m;                                    //(E)
            this.n = X.this.n;                                    //(F)
        }
        public String toString() { 
            return "inner's state: " + this.m + "  " +  this.n; 
        } 
    }

    public X( int mm ) { m = mm; }                                //(G) 
}

class Test {
    public static void main( String[] args ) {
        X x = new X( 100 );                    
        X.Y y = x.new Y();               
        System.out.println( y );                // 100 300
    }
}

NestedInterface.java

interface Drawable {

    class Color {
        private int red;
        private int green;
        private int blue;
        public Color( int r, int g, int b ) {
            red = r;
            green = g;
            blue = b;
        }
    }

    void setColor( Color c );
    void draw();
}

class Rectangle implements Drawable {
    private Color color;  // Color made available by the interface
    private int width;
    private int height;
 
    public Rectangle( int w, int h ) {
        width = w;
        height = h;
    }

    public void setColor( Color c ) { color = c; }

    public void draw() {
        System.out.println( "Invoke code for drawing a rectangle" );
    }

    public static void main( String[] args ) {
        Color col = new Color( 120, 134, 200 );
        Rectangle rect = new Rectangle( 23, 34 );
        rect.setColor( col );
        rect.draw();
    }
}


Maintained by John Loomis, last updated 30 Dec 2006