我不知道这里发生了什么。请解释。代码...
import std.stdio;
void main(string[] args)
{
ClassObject ss1 = new ClassObject("First Class");
writeln(ss1.produce());
ClassObject ss2 = new ClassObject("Some Class");
ClassObject* ss3 = &ss2;
voidclass(ss3);
ClassObject ss4 = new ClassObject("Final Class");
passinterface(ss4);
ClassObject* ss5 = &ss4;
writeln(ss5.produce());
voidinterface(ss5);
}
public void voidclass(void* arg)
{
ClassObject* ss = cast(ClassObject*) arg;
writeln(ss.produce());
writeln(ss.produce);
ClassObject ssnp = *ss;
writeln(ssnp.produce());
writeln(ssnp.produce);
}
public void voidinterface(void* arg)
{
IObject* bob = cast(IObject*) arg;
writeln(bob.produce());
}
public void passinterface(IObject arg)
{
writeln(arg.produce());
}
interface IObject
{
string produce();
}
class ClassObject : IObject
{
protected string _value;
this(string value)
{
this._value = value;
}
public string produce()
{
return this._value;
}
}
输出...
First Class
Some Class
Some Class
Some Class
Some Class
Final Class
Final Class
main.ClassObject
最终输出是怎么回事?我将一个类实例传递给一个需要指针的函数。然后,当我将它转换为接口时,它不尊重接口的方法。但是有些人如何能够告诉这个我通过的类的实例?
可悲的是,我绑定到一个 C 库,所以我必须使用 void* 作为参数类型。所以请不要建议不要使用它。
编辑:编译器信息...
$ dmd -v
DMD64 D Compiler v2.062
Copyright (c) 1999-2012 by Digital Mars written by Walter Bright
Documentation: http://dlang.org/