Checking the type of a up casted class

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.
 
B

Ben Voigt [C++ MVP]

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top