digitalmars.D.learn - Find the heir.
- TodNaz (4/20) Mar 29 2020 The question in this code is: is it possible to track the class
- Stefan Koch (3/26) Mar 29 2020 It is not generally known who has inherited a class from the
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (20/43) Mar 29 2020 The question is a bit unclear - what is whoheir expected to
- WebFreak001 (14/58) Mar 30 2020 to extend on this: typeid(...) returns a TypeInfo so you can
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 ...
Mar 29 2020
On Sunday, 29 March 2020 at 14:04:53 UTC, TodNaz wrote:Hello!It is not generally known who has inherited a class from the parents perspective.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 ...
Mar 29 2020
On Sunday, 29 March 2020 at 14:04:53 UTC, TodNaz wrote:Hello!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"); } -- Simenclass 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 ...
Mar 29 2020
On Sunday, 29 March 2020 at 15:07:37 UTC, Simen Kjærås wrote:On Sunday, 29 March 2020 at 14:04:53 UTC, TodNaz wrote:to extend on this: typeid(...) returns a TypeInfo so you can perform a bit of runtime reflection, see https://dlang.org/phobos/object.html#.TypeInfo To check for exact matches if your class is B or C, you can also use typeid(variable) == typeid(B) if you don't want to compare arbitrary strings. (better code style) There are also libraries to help you do this sort of runtime reflection like witchcraft: https://code.dlang.org/packages/witchcraft or a more current hunt fork, though I don't know yet what it changes: https://github.com/huntlabs/hunt-reflectionHello!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"); } -- Simenclass 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 ...
Mar 30 2020