www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get which derived class an object is if it's stored in an array of its

reply Morimur55 <dgdfgd fdf.com> writes:
I've got a bunch of different classes all derived from the same 
base class sitting in a base[]. I need to check what the derived 
types are of these objects - is there a way to check without 
attempting to cast to every derived type?
Jul 15 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 15 July 2017 at 13:02:52 UTC, Morimur55 wrote:
 is there a way to check without attempting to cast to every 
 derived type?
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.
Jul 15 2017
parent reply Morimur55 <dgdfgd fdf.com> writes:
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:
 is there a way to check without attempting to cast to every 
 derived type?
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. 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 :(
Jul 15 2017
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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?
Jul 15 2017
parent reply Morimur55 <dgdfgd fdf.com> writes:
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 {
Jul 15 2017
parent reply Morimur55 <dgdfgd fdf.com> writes:
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?
Jul 15 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 15 July 2017 at 14:36:40 UTC, Morimur55 wrote:
 ...let me try that again without accidentally sending it before 
 I'd finished...
tab on the web interface is so useful... but so annoying sometimes too.
 ...and I think my problem is actually that redeclared static 
 variables update on the base class if called from a base class 
 function?
Yeah, member variables are always directly attached to the class they are on, no virtualness there. Why are you redeclaring it? A virtual property getter/setter pair might be what you need.
Jul 15 2017
parent Morimur55 <dgdfgd fdf.com> writes:
On Saturday, 15 July 2017 at 14:46:17 UTC, Adam D. Ruppe wrote:
 On Saturday, 15 July 2017 at 14:36:40 UTC, Morimur55 wrote:
 ...let me try that again without accidentally sending it 
 before I'd finished...
tab on the web interface is so useful... but so annoying sometimes too.
 ...and I think my problem is actually that redeclared static 
 variables update on the base class if called from a base class 
 function?
Yeah, member variables are always directly attached to the class they are on, no virtualness there. Why are you redeclaring it? A virtual property getter/setter pair might be what you need.
I'm reading in some objects from yaml, assigning them incremental ids per type so they can be queried and changed at runtime with Lua by another programmer, and then spat back out as yaml when they're done. Mostly redeclaring because I tried it, didn't get a compiler error, so I thought it'd just work the way I imagined it would. What can I say, I like to live dangerously. :p Thanks for your help, I'll put in some virtual properties and hope for the best.
Jul 15 2017
prev sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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 :(
Jul 15 2017