Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Templated Inheritance

Part of TemplatesCategory

Description

This allows the program to decide what a class will inherit from. Note that if your inheriting from an interface the templated class must already implement the needed.

Example

import std.stdio;

void main() {
	(new C!(A)).sayWhat;
	(new C!(B)).iSaidIt;
	(new D!(A)).sayWhat;
	(new D!(B)).iSaidIt;
}

interface iA { void sayWhat(); }
interface iB { void iSaidIt(); }

class A : iA {
	void sayWhat() {
		writefln("Say what?");
	}
}
class B : iB{
	void iSaidIt() {
		writefln("You know what I said.");
	}
}

class C(T) : T {}
class D(T) : T {
	void sayWhat() {
		Stdout("Say what?").newline;
	}
	void iSaidIt() {
		Stdout("You know what I said.").newline;
	}
}

Output

Say what?
You know what I said.
Say what?
You know what I said.