I have a base class and a bunch of subclasses of it.
I have an object of base class and I know that it was an instance of one of the subclasses before, but it was casted to the base class.
I want to know how to discover which subclass it was before. More specifically:
SubClass sc = new SubClass();
BaseClass bc = sc;
if(bs was SubClass) print("bc was SubClass");
How can I write this if correctly? Is it even possible?
Using
isDOES NOT WORK.It does not "discover which subclass it was before", just that it can be cast to the given type.
Take these class definitions:
I can write this code and it gets it WRONG:
It tells me that
bc is IntermediateClass, but it's not. It's aBaseClass.The only way to do it is a direct equality comparison.
This works: