Classes - D Programming Language
Description: D Programming Language
Keywords: D programming language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Forums
  - IRC
  - Community Discord
  - Wiki
  - GitHub
  - Issues
  - Get involved
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# Classes

Contents

1. Access Control
2. Fields
   1. Field Properties
3. Class Properties
   1. Accessing Hidden Fields
4. Super Class
5. Member Functions (a.k.a. Methods)
   1. Objective-C linkage
6. Synchronized Classes
7. Constructors
   1. Delegating Constructors
   2. Implicit Base Class Construction
   3. Class Instantiation
   4. Constructor Attributes
   5. Field initialization inside a constructor
8. Destructors
9. Static Constructors
10. Static Destructors
11. Shared Static Constructors
12. Shared Static Destructors
13. Class Invariants
14. Alias This
15. Scope Classes
16. Abstract Classes
17. Final Classes
18. Nested Classes
    1. Static Nested Classes
    2. Context Pointer
    3. Explicit Instantiation
    4. outer Property
    5. Anonymous Nested Classes
19. Const, Immutable and Shared Classes

The object-oriented features of D all come from classes. The class hierarchy has as its root the class Object. Object defines a minimum level of functionality that each derived class has, and a default implementation for that functionality.

Classes are programmer defined types. Support for classes are what make D an object oriented language, giving it encapsulation, inheritance, and polymorphism. D classes support the single inheritance paradigm, extended by adding support for interfaces. Class objects are instantiated by reference only.

A class can be exported, which means its name and all its non-private members are exposed externally to the DLL or EXE.

A class declaration is defined:

    ClassDeclaration:
        class Identifier ;
        class Identifier BaseClassListopt AggregateBody
        ClassTemplateDeclaration

    BaseClassList:
        : SuperClassOrInterface
        : SuperClassOrInterface , Interfaces

    SuperClassOrInterface:
        BasicType

    Interfaces:
        Interface
        Interface , Interfaces

    Interface:
        BasicType

A class consists of:

- a super class
- interfaces
- dynamic fields
- static fields
- nested classes
- member functions

- static member functions
- Virtual Functions
- Constructors
- Destructors
- Class Invariants
- Operator Overloading

- Alias This
- other declarations (see DeclDef)

A class is defined:

    class Foo
    {
        ... members ...
    }

Note that there is no trailing ; after the closing } of the class definition. It is also not possible to declare a variable var like:

    class Foo { } var;

Instead:

    class Foo { }
    Foo var;

## Access Control

Access to class members is controlled using visibility attributes. The default visibility attribute is public.

## Fields

Class members are always accessed with the . operator.

Members of a base class can be accessed by prepending the name of the base class followed by a dot:

    class A { int a; int a2;}
    class B : A { int a; }

    void foo(B b)
    {
        b.a = 3;   // accesses field B.a
        b.a2 = 4;  // accesses field A.a2
        b.A.a = 5; // accesses field A.a
    }

The D compiler is free to rearrange the order of fields in a class to optimally pack them in an implementation-defined manner. Consider the fields much like the local variables in a function - the compiler assigns some to registers and shuffles others around all to get the optimal stack frame layout. This frees the code designer to organize the fields in a manner that makes the code more readable rather than being forced to organize it according to machine optimization rules. Explicit control of field layout is provided by struct/union types, not classes.

Fields of extern(Objective-C) classes have a dynamic offset. That means that the base class can change (add or remove instance variables) without the subclasses needing to recompile or relink.

### Field Properties

The .offsetof property gives the offset in bytes of the field from the beginning of the class instantiation. .offsetof is not available for fields of extern(Objective-C) classes due to their fields having a dynamic offset. .offsetof can only be applied to expressions which produce the type of the field itself, not the class type:

    class Foo
    {
        int x;
    }
    ...
    void test(Foo foo)
    {
        size_t o;

        o = Foo.x.offsetof; // error, Foo.x needs a 'this' reference
        o = foo.x.offsetof; // ok
    }

## Class Properties

The .tupleof property is an lvalue sequence of all the non-static fields in the class, excluding the hidden fields and the fields in the base class.

The order of the fields in the tuple matches the order in which the fields are declared.

Note: .tupleof is not available for extern(Objective-C) classes due to their fields having a dynamic offset.

    class Foo { int x; long y; }

    static assert(__traits(identifier, Foo.tupleof[0]) == "x");
    static assert(is(typeof(Foo.tupleof)[1] == long));

    void main()
    {
        import std.stdio;

        auto foo = new Foo;
        foo.tupleof[0] = 1; // set foo.x to 1
        foo.tupleof[1] = 2; // set foo.y to 2
        foreach (ref x; foo.tupleof)
            x++;
        assert(foo.x == 2);
        assert(foo.y == 3);

        auto bar = new Foo;
        bar.tupleof = foo.tupleof; // copy fields
        assert(bar.x == 2);
        assert(bar.y == 3);
    }

### Accessing Hidden Fields

The .outer property for a nested class instance provides either the parent class instance, or the parent function's context pointer when there is no parent class.

The properties .__vptr and .__monitor give access to the class object's vtbl[] and monitor, respectively, but should not be used in user code.

## Super Class

All classes inherit from a super class. If one is not specified, it inherits from Object. Object forms the root of the D class inheritance hierarchy.

## Member Functions (a.k.a. Methods)

Non-static member functions have an extra hidden parameter called this through which the class object's other members can be accessed.

Non-static member functions can have, in addition to the usual FunctionAttributes, the attributes const, immutable, shared, inout, scope or return scope. These attributes apply to the hidden this parameter.

    class C
    {
        int a;
        void foo() const
        {
            a = 3; // error, 'this' is const
        }
        void foo() immutable
        {
            a = 3; // error, 'this' is immutable
        }
        C bar() @safe scope
        {
            return this; // error, 'this' is scope
        }
    }

### Objective-C linkage

Static member functions with Objective-C linkage also have an extra hidden parameter called this through which the class object's other members can be accessed.

Member functions with Objective-C linkage have an additional hidden, anonymous, parameter which is the selector the function was called with.

Static member functions with Objective-C linkage are placed in a hidden nested metaclass as non-static member functions.

## Synchronized Classes

All member functions of synchronized classes are synchronized. A static member function is synchronized on the classinfo object for the class, which means that one monitor is used for all static member functions for that synchronized class. For non-static functions of a synchronized class, the monitor used is part of the class object. For example:

    synchronized class Foo
    {
        void bar() { ...statements... }
    }

is equivalent to (as far as the monitors go):

    synchronized class Foo
    {
        void bar()
        {
            synchronized (this) { ...statements... }
        }
    }

Member functions of non-synchronized classes can be individually marked as synchronized.

    class Foo
    {
        synchronized void foo() { }  // foo is synchronized
    }

Member fields of a synchronized class cannot be public:

    synchronized class Foo
    {
        int foo;  // disallowed: public field
    }

    synchronized class Bar
    {
        private int bar;  // ok
    }

The synchronized attribute can only be applied to classes, structs cannot be marked to be synchronized.

## Constructors

    Constructor:
        this Parameters MemberFunctionAttributesopt FunctionBody
        ConstructorTemplate

Fields are by default initialized to the default initializer for their type (usually 0 for integer types and NAN for floating point types). If the field declaration has an optional Initializer that will be used instead of the default.

    class Abc
    {
        int a;      // default initializer for a is 0
        long b = 7; // default initializer for b is 7
        float f;    // default initializer for f is NAN
    }

The Initializer is evaluated at compile time.

This initialization is done before any constructors are called.

Constructors are defined with a function name of this and have no return value:

    class Foo
    {
        this(int x)  // declare constructor for Foo
        {   ...
        }
        this()
        {   ...
        }
    }

Base class construction is done by calling the base class constructor by the name super:

    class A { this(int y) { } }

    class B : A
    {
        int j;
        this()
        {
            ...
            super(3);  // call base constructor A.this(3)
            ...
        }
    }

### Delegating Constructors

A constructor can call another constructor for the same class in order to share common initializations. This is called a delegating constructor:

    class C
    {
        int j;
        this()
        {
            ...
        }
        this(int i)
        {
            this(); // delegating constructor call
            j = i;
        }
    }

The following restrictions apply:

1. It is illegal for constructors to mutually call each other.

       this() { this(1); }
       this(int i) { this(); } // illegal, cyclic constructor calls

   Implementation Defined: The compiler is not required to detect cyclic constructor calls.
   Undefined Behavior: If the program executes with cyclic constructor calls.
2. If a constructor's code contains a delegating constructor call, all possible execution paths through the constructor must make exactly one delegating constructor call:

       this() { a || super(); }       // illegal

       this() { (a) ? this(1) : super(); }     // ok

       this()
       {
           for (...)
           {
               super();  // illegal, inside loop
           }
       }

3. It is illegal to refer to this implicitly or explicitly prior to making a delegating constructor call.
4. Delegating constructor calls cannot appear after labels.

### Implicit Base Class Construction

If there is no constructor for a class, but there is a constructor for the base class, a default constructor is implicitly generated with the form:

    this() { }

If no calls to a delegating constructor or super appear in a constructor, and the base class has a nullary constructor, a call to super() is inserted at the beginning of the constructor. If that base class has a constructor that requires arguments and no nullary constructor, a matching call to super is required.

### Class Instantiation

Instances of class objects are created with a NewExpression:

    A a = new A(3);

The following steps happen:

1. Storage is allocated for the object. If this fails, rather than return null, an OutOfMemoryError is thrown. Thus, tedious checks for null references are unnecessary.
2. The raw data is statically initialized using the values provided in the class definition. The pointer to the vtbl[] (the array of pointers to virtual functions) is assigned. Constructors are passed fully formed objects for which virtual functions can be called. This operation is equivalent to doing a memory copy of a static version of the object onto the newly allocated one.
3. If there is a constructor defined for the class, the constructor matching the argument list is called.
4. If a delegating constructor is not called, a call to the base class's default constructor is issued.
5. The body of the constructor is executed.
6. If class invariant checking is turned on, the class invariant is called at the end of the constructor.

### Constructor Attributes

Constructors can have one of these member function attributes: const, immutable, and shared. Construction of qualified objects will then be restricted to the implemented qualified constructors.

    class C
    {
        this();   // non-shared mutable constructor
    }

    // create mutable object
    C m = new C();

    // create const object using mutable constructor
    const C c2 = new const C();

    // a mutable constructor cannot create an immutable object
    // immutable C i = new immutable C();

    // a mutable constructor cannot create a shared object
    // shared C s = new shared C();

Constructors can be overloaded with different attributes.

    class C
    {
        this();               // non-shared mutable constructor
        this() shared;        // shared mutable constructor
        this() immutable;     // immutable constructor
    }

    C m = new C();
    shared s = new shared C();
    immutable i = new immutable C();

#### Pure Constructors

If the constructor can create a unique object (e.g. if it is pure), the object can be implicitly convertible to any qualifiers.

    class C
    {
        this() pure;
        // Based on the definition, this creates a mutable object. But the
        // created object cannot contain any mutable global data.
        // Therefore the created object is unique.

        this(int[] arr) immutable pure;
        // Based on the definition, this creates an immutable object. But
        // the argument int[] never appears in the created object so it
        // isn't implicitly convertible to immutable. Also, it cannot store
        // any immutable global data.
        // Therefore the created object is unique.
    }

    immutable i = new immutable C();           // this() pure is called
    shared s = new shared C();                 // this() pure is called
    C m = new C([1,2,3]);       // this(int[]) immutable pure is called

### Field initialization inside a constructor

In a constructor body, the first instance of field assignment is its initialization.

    class C
    {
        int num;
        this()
        {
            num = 1;  // initialization
            num = 2;  // assignment
        }
    }

If the field type has an opAssign method, it will not be used for initialization.

    struct A
    {
        this(int n) {}
        void opAssign(A rhs) {}
    }
    class C
    {
        A val;
        this()
        {
            val = A(1);  // val is initialized to the value of A(1)
            val = A(2);  // rewritten to val.opAssign(A(2))
        }
    }

If the field type is not mutable, multiple initialization will be rejected.

    class C
    {
        immutable int num;
        this()
        {
            num = 1;  // OK
            num = 2;  // Error: multiple field initialization
        }
    }

If the field is initialized on one path, it must be initialized on all paths.

    class C
    {
        immutable int num;
        immutable int ber;
        this(int i)
        {
            if (i)
                num = 3;   // initialization
            else
                num = 4;   // initialization
        }
        this(long j)
        {
            j ? (num = 3) : (num = 4); // ok
            j || (ber = 3);  // error, intialized on only one path
            j && (ber = 3);  // error, intialized on only one path
        }
    }

A field initialization may not appear in a loop or after a label.

    class C
    {
        immutable int num;
        immutable string str;
        this()
        {
            foreach (i; 0..2)
            {
                num = 1;    // Error: field initialization not allowed in loops
            }
            size_t i = 0;
        Label:
            str = "hello";  // Error: field initialization not allowed after labels
            if (i++ < 2)
                goto Label;
        }
    }

If a field's type has disabled default construction, then it must be initialized in the constructor.

    struct S { int y; @disable this(); }

    class C
    {
        S s;
        this(S t) { s = t; }    // ok
        this(int i) { this(); } // ok
        this() { }              // error, s not initialized
    }

## Destructors

    Destructor:
        ~ this ( ) MemberFunctionAttributesopt FunctionBody

The garbage collector calls the destructor function when the object is deleted. The syntax is:

    class Foo
    {
        ~this() // destructor for Foo
        {
        }
    }

There can be only one destructor per class, the destructor does not have any parameters, and has no attributes. It is always virtual.

The destructor is expected to release any resources held by the object.

The program can explicitly inform the garbage collector that an object is no longer referred to with destroy, and then the garbage collector calls the destructor immediately. The destructor is guaranteed to never be called twice.

The destructor for the super class automatically gets called when the destructor ends. There is no way to call the super destructor explicitly.

The garbage collector is not guaranteed to run the destructor for all unreferenced objects. Furthermore, the order in which the garbage collector calls destructors for unreferenced objects is not specified. This means that when the garbage collector calls a destructor for an object of a class that has members which are references to garbage collected objects, those references may no longer be valid. This means that destructors cannot reference sub objects. This rule does not apply to auto objects or objects destructed with destroy, as the destructor is not being run by the garbage collector, meaning all references are valid.

Objects referenced from the data segment never get collected by the GC.

## Static Constructors

    StaticConstructor:
        static this ( ) MemberFunctionAttributesopt ;
        static this ( ) MemberFunctionAttributesopt FunctionBody

A static constructor is a function that performs initializations of thread local data before the main() function gets control for the main thread, and upon thread startup.

Static constructors are used to initialize static class members with values that cannot be computed at compile time.

Static constructors in other languages are built implicitly by using member initializers that can't be computed at compile time. The trouble with this stems from not having good control over exactly when the code is executed, for example:

    class Foo
    {
        static int a = b + 1;
        static int b = a * 2;
    }

What values do a and b end up with, what order are the initializations executed in, what are the values of a and b before the initializations are run, is this a compile error, or is this a runtime error? Additional confusion comes from it not being obvious if an initializer is static or dynamic.

D makes this simple. All member initializations must be determinable by the compiler at compile time, hence there is no order-of-evaluation dependency for member initializations, and it is not possible to read a value that has not been initialized. Dynamic initialization is performed by a static constructor, defined with a special syntax static this().

    class Foo
    {
        static int a;         // default initialized to 0
        static int b = 1;
        static int c = b + a; // error, not a constant initializer

        static this()    // static constructor
        {
            a = b + 1;          // a is set to 2
            b = a * 2;          // b is set to 4
        }
    }

If main() or the thread returns normally, (does not throw an exception), the static destructor is added to the list of functions to be called on thread termination.

Static constructors have empty parameter lists.

Static constructors within a module are executed in the lexical order in which they appear. All the static constructors for modules that are directly or indirectly imported are executed before the static constructors for the importer.

The static in the static constructor declaration is not an attribute, it must appear immediately before the this:

    class Foo
    {
        static this() { ... } // a static constructor
        static private this() { ... } // not a static constructor
        static
        {
            this() { ... }      // not a static constructor
        }
        static:
            this() { ... }      // not a static constructor
    }

## Static Destructors

    StaticDestructor:
        static ~ this ( ) MemberFunctionAttributesopt ;
        static ~ this ( ) MemberFunctionAttributesopt FunctionBody

A static destructor is defined as a special static function with the syntax static ~this().

    class Foo
    {
        static ~this() // static destructor
        {
        }
    }

A static destructor gets called on thread termination, but only if the static constructor completed successfully. Static destructors have empty parameter lists. Static destructors get called in the reverse order that the static constructors were called in.

The static in the static destructor declaration is not an attribute, it must appear immediately before the ~this:

    class Foo
    {
        static ~this() { ... }  // a static destructor
        static private ~this() { ... } // not a static destructor
        static
        {
            ~this() { ... }  // not a static destructor
        }
        static:
            ~this() { ... }  // not a static destructor
    }

## Shared Static Constructors

    SharedStaticConstructor:
        shared static this ( ) MemberFunctionAttributesopt ;
        shared static this ( ) MemberFunctionAttributesopt FunctionBody

Shared static constructors are executed before any StaticConstructors, and are intended for initializing any shared global data.

## Shared Static Destructors

    SharedStaticDestructor:
        shared static ~ this ( ) MemberFunctionAttributesopt ;
        shared static ~ this ( ) MemberFunctionAttributesopt FunctionBody

Shared static destructors are executed at program termination in the reverse order that SharedStaticConstructors were executed.

## Class Invariants

    Invariant:
        invariant ( ) BlockStatement
        invariant BlockStatement
        invariant ( AssertArguments ) ;

Class Invariants specify the relationships among the members of a class instance. Those relationships must hold for any interactions with the instance from its public interface.

The invariant is in the form of a const member function. The invariant is defined to hold if all the AssertExpressions within the invariant that are executed succeed.

    class Date
    {
        this(int d, int h)
        {
            day = d;    // days are 1..31
            hour = h;   // hours are 0..23
        }

        invariant
        {
            assert(1 <= day && day <= 31);
            assert(0 <= hour && hour < 24);
        }

      private:
        int day;
        int hour;
    }

Any class invariants for base classes are applied before the class invariant for the derived class.

There may be multiple invariants in a class. They are applied in lexical order.

Class Invariants must hold at the exit of the class constructor (if any), and at the entry of the class destructor (if any).

Class Invariants must hold at the entry and exit of all public or exported non-static member functions. The order of application of invariants is:

1. preconditions
2. invariant
3. function body
4. invariant
5. postconditions

If the invariant does not hold, then the program enters an invalid state.

Implementation Defined:

1. Whether the class Invariant is executed at runtime or not. This is typically controlled with a compiler switch.
2. The behavior when the invariant does not hold is typically the same as for when AssertExpressions fail.

Undefined Behavior: happens if the invariant does not hold and execution continues.

Public or exported non-static member functions cannot be called from within an invariant.

    class Foo
    {
        public void f() { }
        private void g() { }

        invariant
        {
            f();  // error, cannot call public member function from invariant
            g();  // ok, g() is not public
        }
    }

Best Practices:

1. Do not indirectly call exported or public member functions within a class invariant, as this can result in infinite recursion.
2. Avoid reliance on side effects in the invariant. as the invariant may or may not be executed.
3. Avoid having mutable public fields of classes with invariants, as then the invariant cannot verify the public interface.

## Alias This

    AliasThis:
        alias Identifier this ;

An AliasThis declaration names a member to subtype. The Identifier names that member.

A class or struct can be implicitly converted to the AliasThis member.

    struct S
    {
        int x;
        alias x this;
    }

    int foo(int i) { return i * 2; }

    void main()
    {
        S s;
        s.x = 7;
        int i = -s;
        assert(i == -7);
        i = s + 8;
        assert(i == 15);
        i = s + s;
        assert(i == 14);
        i = 9 + s;
        assert(i == 16);
        i = foo(s);  // implicit conversion to int
        assert(i == 14);
    }

If the member is a class or struct, undefined lookups will be forwarded to the AliasThis member.

    struct Foo
    {
        int baz = 4;
        int get() { return 7; }
    }

    class Bar
    {
        Foo foo;
        alias foo this;
    }

    void main()
    {
        auto bar = new Bar;
        int i = bar.baz;
        assert(i == 4);
        i = bar.get();
        assert(i == 7);
    }

If the Identifier refers to a property member function with no parameters, conversions and undefined lookups are forwarded to the return value of the function.

    struct S
    {
        int x;
        @property int get()
        {
            return x * 2;
        }
        alias get this;
    }

    void main()
    {
        S s;
        s.x = 2;
        int i = s;
        assert(i == 4);
    }

If an aggregate declaration defines an opCmp or opEquals method, it will take precedence to that of the aliased this member. Note that, unlike an opCmp method, an opEquals method is implicitly defined for a struct declaration if a user defined one isn't provided; this means that if the aliased this member opEquals is preferred it should be explicitly defined:

    struct S
    {
        int a;
        bool opEquals(S rhs) const
        {
            return this.a == rhs.a;
        }
    }

    struct T
    {
        int b;
        S s;
        alias s this;
    }

    void main()
    {
        S s1, s2;
        T t1, t2;

        assert(s1 == s2);      // calls S.opEquals
        assert(t1 == t2);      // calls compiler generated T.opEquals that implements member-wise equality

        assert(s1 == t1);      // calls s1.opEquals(t1.s);
        assert(t1 == s1);      // calls t1.s.opEquals(s1);
    }

    struct U
    {
        int a;
        bool opCmp(U rhs) const
        {
            return this.a < rhs.a;
        }
    }

    struct V
    {
        int b;
        U u;
        alias u this;
    }

    void main()
    {
        U u1, u2;
        V v1, v2;

        assert(!(u1 < u2));    // calls U.opCmp
        assert(!(v1 < v2));    // calls U.opCmp because V does not define an opCmp method
                               // so the alias this of v1 is employed; U.opCmp expects a
                               // paramter of type U, so alias this of v2 is used

        assert(!(u1 < v1));    // calls u1.opCmp(v1.u);
        assert(!(v1 < u1));    // calls v1.u.opCmp(v1);
    }

Attributes are ignored for AliasThis.

Multiple AliasThis are currently not allowed.

## Scope Classes

Note: Scope classes have been recommended for deprecation.

A scope class is a class with the scope attribute, as in:

    scope class Foo { ... }

The scope characteristic is inherited, so any classes derived from a scope class are also scope.

A scope class reference can only appear as a function local variable. It must be declared as being scope:

    scope class Foo { ... }

    void func()
    {
        Foo f;    // error, reference to scope class must be scope
        scope Foo g = new Foo(); // correct
    }

When a scope class reference goes out of scope, the destructor (if any) for it is automatically called. This holds true even if the scope was exited via a thrown exception.

## Abstract Classes

An abstract member function must be overridden by a derived class. Only virtual member functions may be declared abstract; non-virtual member functions and free-standing functions cannot be declared abstract.

A class is abstract if any of its virtual member functions are declared abstract or if they are defined within an abstract attribute. Note that an abstract class may also contain non-virtual member functions. Abstract classes cannot be instantiated directly. They can only be instantiated as a base class of another, non-abstract, class.

    class C
    {
        abstract void f();
    }

    auto c = new C; // error, C is abstract

    class D : C {}

    auto d = new D; // error, D is abstract

    class E : C
    {
        override void f() {}
    }

    auto e = new E; // OK

Member functions declared as abstract can still have function bodies. This is so that even though they must be overridden, they can still provide ‘base class functionality’, e.g. through super.foo() in a derived class. Note that the class is still abstract and cannot be instantiated directly.

A class can be declared abstract:

    abstract class A
    {
        // ...
    }

    auto a = new A; // error, A is abstract

    class B : A {}

    auto b = new B; // OK

## Final Classes

Final classes cannot be subclassed:

    final class A { }
    class B : A { }  // error, class A is final

Methods of a final class are always final.

## Nested Classes

A nested class is a class that is declared inside the scope of a function or another class. A nested class has access to the variables and other symbols of the classes and functions it is nested inside:

    class Outer
    {
        int m;

        class Inner
        {
            int foo()
            {
                return m;   // Ok to access member of Outer
            }
        }
    }

    void func()
    {
        int m;

        class Inner
        {
            int foo()
            {
                return m; // Ok to access local variable m of func()
            }
        }
    }

### Static Nested Classes

If a nested class has the static attribute, then it can not access variables of the enclosing scope that are local to the stack or need a this reference:

    class Outer
    {
        int m;
        static int n;

        static class Inner
        {
            int foo()
            {
                return m;   // Error, Inner is static and m needs a this
                return n;   // Ok, n is static
            }
        }
    }

    void func()
    {
        int m;
        static int n;

        static class Inner
        {
            int foo()
            {
                return m;   // Error, Inner is static and m is local to the stack
                return n;   // Ok, n is static
            }
        }
    }

### Context Pointer

Non-static nested classes work by containing an extra hidden member (called the context pointer) that is the frame pointer of the enclosing function if it is nested inside a function, or the this reference of the enclosing class's instance if it is nested inside a class.

When a non-static nested class is instantiated, the context pointer is assigned before the class's constructor is called, therefore the constructor has full access to the enclosing variables. A non-static nested class can only be instantiated when the necessary context pointer information is available:

    class Outer
    {
        class Inner { }

        static class SInner { }
    }

    void main()
    {
        Outer o = new Outer;        // Ok
        //Outer.Inner oi = new Outer.Inner; // Error, no 'this' for Outer
        Outer.SInner os = new Outer.SInner; // Ok
    }

    void main()
    {
        class Nested { }

        Nested n = new Nested;      // Ok

        static f()
        {
            //Nested sn = new Nested; // Error, no 'this' for Nested
        }
    }

### Explicit Instantiation

A this reference can be supplied to the creation of an inner class instance by prefixing it to the NewExpression:

    class Outer
    {
        int a;

        class Inner
        {
            int foo()
            {
                return a;
            }
        }
    }

    void main()
    {
        Outer o = new Outer;
        o.a = 3;
        Outer.Inner oi = o.new Inner;
        assert(oi.foo() == 3);
    }

Here o supplies the this reference to the inner class instance of Outer.

### outer Property

For a nested class instance, the .outer property provides the this reference of the enclosing class's instance. If there is no accessible parent class instance, the property provides a void* to the enclosing function frame.

    class Outer
    {
        class Inner1
        {
            Outer getOuter()
            {
                return this.outer;
            }
        }

        void foo()
        {
            Inner1 i = new Inner1;
            assert(i.getOuter() is this);
        }
    }

    class Outer
    {
        void bar()
        {
            // x is referenced from nested scope, so
            // bar makes a closure environment.
            int x = 1;

            class Inner2
            {
                Outer getOuter()
                {
                    x = 2;
                    // The Inner2 instance has access to the function frame
                    // of bar as a static frame pointer, but .outer returns
                    // the enclosing Outer class instance property.
                    return this.outer;
                }
            }

            Inner2 i = new Inner2;
            assert(i.getOuter() is this);
        }
    }

    class Outer
    {
        // baz cannot access an instance of Outer
        static void baz()
        {
            // make a closure environment
            int x = 1;

            class Inner3
            {
                void* getOuter()
                {
                    x = 2;
                    // There's no accessible enclosing class instance, so the
                    // .outer property returns the function frame of baz.
                    return this.outer;
                }
            }

            Inner3 i = new Inner3;
            assert(i.getOuter() !is null);
        }
    }

### Anonymous Nested Classes

An anonymous nested class is both defined and instantiated with a NewAnonClassExpression:

    NewAnonClassExpression:
        new class ConstructorArgsopt AnonBaseClassListopt AggregateBody

    ConstructorArgs:
        ( ArgumentListopt )

    AnonBaseClassList:
        SuperClassOrInterface
        SuperClassOrInterface , Interfaces

which is equivalent to:

    class Identifier : AnonBaseClassList AggregateBody
    // ...
    new Identifier ConstructorArgs

where Identifier is the name generated for the anonymous nested class.

    interface I
    {
        void foo();
    }

    auto obj = new class I
    {
        void foo()
        {
            writeln("foo");
        }
    };
    obj.foo();

## Const, Immutable and Shared Classes

If a ClassDeclaration has a const, immutable or shared storage class, then it is as if each member of the class was declared with that storage class. If a base class is const, immutable or shared, then all classes derived from it are also const, immutable or shared.

Structs and Unions
Interfaces
Copyright © 1999-2023 by the D Language Foundation | Page generated by Ddoc on Sat Apr 1 13:10:00 2023