原文
import std.exception;
import std.stdio;
abstract class B
{
B opCall() {
throw new Exception("NotImplemented");
}
}
class A : B
{
override B opCall() {
return new A();
}
}
int main() {
B a = new A();
a();
readln();
return 0;
}
似乎工作正常.
你在A中覆盖opCall
的实现,因此调用opCall
时,它调用A中实现.B
的opCall
不在vtable
中.
它返回了A对象
.
import std.stdio;
abstract class B
{
B opCall() {
throw new Exception("NotImplemented");
}
}
class A : B
{
override B opCall() {
return new A();
}
}
int main() {
B a = new A();
auto got = a();
assert(got !is null);
writeln(got);
return 0;
}
是我未在子类中实现.