Interface factory
luke
lukas.laedrach at bluewin.ch
Tue Aug 14 08:28:03 PDT 2007
Hi everybody
I wrote the following code to be able to write unit tests for interfaces.
--------------------------------------------------------------
module test;
import std.stdio;
interface I
{
void doSomething();
}
class Iimplementation : I
{
void doSomething()
{
writefln("Hello world!");
}
}
typedef I function() FactoryType;
Iimplementation Factory()
{
return new Iimplementation();
}
void main()
{
auto factory = &Factory;
auto ifactory = cast(FactoryType)factory;
I i = factory();
i.doSomething(); // Prints 'Hello world!'
i = ifactory();
i.doSomething(); // Prints 'InterfaceClassTest.Iimplementation' <<< ??
}
--------------------------------------------------------------
compiled using dmd 1.015 on windows
The problem is marked in the source. When i create the instance using
factory the program works as expected, but when i cast it just prints
garbage(?).
Is this a normal behavior? If it is, how can i create an instance of
an interface using a factory?
More information about the Digitalmars-d-learn
mailing list