Get which derived class an object is if it's stored in an array of its base class

Morimur55 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 15 07:36:40 PDT 2017


On Saturday, 15 July 2017 at 14:26:30 UTC, Morimur55 wrote:
> On Saturday, 15 July 2017 at 14:04:17 UTC, Adam D. Ruppe wrote:
>> On Saturday, 15 July 2017 at 13:45:40 UTC, Morimur55 wrote:
>>> 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.
>>
>> That means you're doing something wrong. What does your base 
>> class method look like? Is it a template?
>
> class Base {
>     private static int idcounter = 0;
>     int nextid(){ return ++idcounter }
> }
>
> class Derrived {

...let me try that again without accidentally sending it before 
I'd finished...

class Base {
     private static int idcounter = 0;
     int nextid(){ return ++idcounter }
} //nextid updates Base.idcounter

class Derived {
     private static int idcounter = 1000;
     private static int derivedcounter = 0;
     override int nextid(){
         derivedcounter++;
         return ++idcounter
     }
} //nextid updates Derived.idcounter and Derived.derivedcounter

class Derived2 {
     private static int idcounter = 2000;
} //nextid updates Base.idcounter :(

...and I think my problem is actually that redeclared static 
variables update on the base class if called from a base class 
function?


More information about the Digitalmars-d-learn mailing list