Automatic method overriding in sub-classes
Ali Çehreli via Digitalmars-d
digitalmars-d at puremagic.com
Mon Oct 26 17:07:36 PDT 2015
On 10/26/2015 04:25 PM, Tofu Ninja wrote:
> ##################################
> class A
> {
> void foo(this T)() { writeln(T.stringof); }
> void bar(auto override this T)() { writeln(T.stringof); }
> }
>
> class B : A {}
>
> void main()
> {
> A a = new A();
> a.foo(); // prints "A"
> a.bar(); // prints "A"
>
> B b = new B();
> b.foo(); // prints "B"
> b.bar(); // prints "B"
>
> A c = new B();
> c.foo(); // prints "A"
> c.bar(); // prints "B" <-- main advantage, method is virtual
> }
> ##################################
I don't understand all uses of the request but typeid() returns a
TypeInfo reference, which is about the actual type of an object. The
following change produces the expected output in this case (except, it
has the added module name):
void bar() { writeln(typeid(this).name); }
The output:
A
deneme.A
B
deneme.B
A
deneme.B
Ali
More information about the Digitalmars-d
mailing list