Find the heir.

Simen Kjærås simen.kjaras at gmail.com
Sun Mar 29 15:07:37 UTC 2020


On Sunday, 29 March 2020 at 14:04:53 UTC, TodNaz wrote:
> Hello!
>>class A
>>{
>>    ...
>>}
>>
>>class B : A
>>{
>>    ...
>>}
>>
>>class C : A
>>{
>>    ...
>>}
>>
>>A example1;
>>B example2 = new B(...);
>>A = example2;
>>auto heir = A.whoheir(); ///
>
> The question in this code is: is it possible to track the class 
> inheritor? Or is it beyond D?
> Sorry if the question is fool ...

The question is a bit unclear - what is whoheir expected to 
return? This is one way that may or may not fulfill your 
requirements:

module foo;
class A {
     string whoheir() {
         return typeid(this).name;
     }
}
class B : A {}
class C : A {}

unittest {
     A a = new B();
     assert(a.whoheir == "foo.B");
     a = new C();
     assert(a.whoheir == "foo.C");
}

--
   Simen


More information about the Digitalmars-d-learn mailing list