digitalmars.D.learn - Get which derived class an object is if it's stored in an array of its
- Morimur55 (4/4) Jul 15 2017 I've got a bunch of different classes all derived from the same
- Adam D. Ruppe (5/7) Jul 15 2017 The `typeid(obj)` will give the type... but why do you need it?
- Morimur55 (10/17) Jul 15 2017 Well I want to cast to the derived type so I can use a method
- Adam D. Ruppe (3/8) Jul 15 2017 That means you're doing something wrong. What does your base
- Morimur55 (6/14) Jul 15 2017 class Base {
- Morimur55 (21/36) Jul 15 2017 ...let me try that again without accidentally sending it before
- Adam D. Ruppe (7/12) Jul 15 2017 tab on the web interface is so useful... but so annoying
- Morimur55 (10/22) Jul 15 2017 I'm reading in some objects from yaml, assigning them incremental
- Nicholas Wilson (6/24) Jul 15 2017 It should work, please post some test code.
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
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
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: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 :(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
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
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:class Base { private static int idcounter = 0; int nextid(){ return ++idcounter } } class Derrived {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
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:...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?On Saturday, 15 July 2017 at 13:45:40 UTC, Morimur55 wrote:class Base { private static int idcounter = 0; int nextid(){ return ++idcounter } } class Derrived {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
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
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: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....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
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: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 BOn Saturday, 15 July 2017 at 13:02:52 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.[...]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.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