Checking the type of a up casted class

  • Thread starter Thread starter DaTurk
  • Start date Start date
D

DaTurk

Hi, I want to check the type of a derived unmanaged class, upcasted to
it's base class in CLI. Basically, I have several c++ classes that
derive from a single abstract base class. I'm passing the base class
in, and I want to check the type, and cast it later. Please advise.
 
DaTurk said:
Hi, I want to check the type of a derived unmanaged class, upcasted to
it's base class in CLI. Basically, I have several c++ classes that
derive from a single abstract base class. I'm passing the base class
in, and I want to check the type, and cast it later. Please advise.

If you want to print out the name of the actual runtime dynamic type for
debugging purposes:

object->GetType()->Name

If you want to run some code depending on what type it is, use a virtual
function in the base class. If you absolutely can't change the base class,
use dynamic_cast<DerivedType^>(object), it will be a DerivedType^ or nullptr
depending on whether the object is really a DerivedType (or derived from
it).
 
Back
Top