Get which derived class an object is if it's stored in an array of its base class
Nicholas Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jul 15 07:09:42 PDT 2017
On Saturday, 15 July 2017 at 13:45:40 UTC, Morimur55 wrote:
> On Saturday, 15 July 2017 at 13:12:49 UTC, Adam D. Ruppe wrote:
>> On Saturday, 15 July 2017 at 13:02:52 UTC, Morimur55 wrote:
>>> [...]
>>
>> The `typeid(obj)` will give the type... but why do you need
>> it? The classinfo returned by that doesn't give a lot of info.
>>
>> Casting is how you actually get the object, though you might
>> be better off putting the necessary methods in the base class.
>
> Well I want to cast to the derived type so I can use a method
> that's defined in the base class, but is overridden in several
> of the derived types... and calling it without a cast seems to
> give me the base type functionality, but I'd like the derived
> type functionality when it's defined.
It should work, please post some test code.
We have a syntax If you want to call a method from a different
class on an object:
A a;
a.B.foo(); // call a.foo as if `a` were a B
> I have a lot of derived types, some of which may be added in
> future by someone else... was hoping something like
> `cast(typeid(obj))` was going to work... but my compiler
> doesn't like that :(
More information about the Digitalmars-d-learn
mailing list