On Wednesday, 20 November 2019 at 10:05:11 UTC, zoujiaqing wrote:
import std.stdio;
class A
{
this(T)(T t)
{
}
void write()
{
T _this = cast(T) this;
writeln(this.v);
}
}
class B : A
{
string v = "hello";
}
void main()
{
auto b = new B;
writeln(b.write()); // print hello
}
You can use a template this parameter [1], like this:
import std.stdio;
class A
{
void write(this T)()
{
T self = cast(T) this;
writeln(self.v);
}
}
class B : A
{
string v = "hello";
}
void main()
{
auto b = new B;
b.write();
}
[1] https://dlang.org/spec/template.html#template_this_parameter
--
/Jacob Carlborg