How to get child class Type and members from parent class?
Jacob Carlborg
doob at me.com
Wed Nov 20 13:46:07 UTC 2019
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
More information about the Digitalmars-d-learn
mailing list