Get object's type from pointer to base class at runtime

There's something like typeid http://en.cppreference.com/w/cpp/language/typeid, which applied to polymorphic expression will evaluate in a runtime to its type representation.

Following wiki example: https://en.wikipedia.org/wiki/Run-time_type_information#dynamic_cast

#include <iostream> 
#include <typeinfo>    // for 'typeid'

class Person {
public:
   virtual ~Person() {}
};

class Employee : public Person {
};

int main() 
{
    Person person;
    Employee employee;
    Person* ptr = &employee;
    Person& ref = employee;
    // The string returned by typeid::name is implementation-defined

    // Person (statically known at compile-time)
    std::cout << typeid(person).name() << std::endl;   

    // Employee (statically known at compile-time)
    std::cout << typeid(employee).name() << std::endl; 

    // Person* (statically known at compile-time)
    std::cout << typeid(ptr).name() << std::endl;      

    /* Employee (looked up dynamically at run-time
     * because it is the dereference of a
     * pointer to a polymorphic class) */
    std::cout << typeid(*ptr).name() << std::endl;     

    // Employee (references can also be polymorphic)        
    std::cout << typeid(ref).name() << std::endl;      
}

There is a typeid operator, which returns an instance of std::type_info, with which you can get the name of the type.

Not sure if that will help you though. First, the returned name is not guaranteed to be the same across implementations. Second - what would you do once you have the name? You'd compare it with your pre-defined names probably, but that is probably slower than a bunch of dynamic_cast's.

Without type support built into your Base class or a new intermediate layer of hierarchy, dynamic_cast is your best choice. In reality it will be very fast (usually just a single compare instruction).

By intermediate layer I mean:

class Base {
public:
    virtual ~Base() {}
};

class T1Base : public Base {};
class T2Base : public Base {};

template <typename T> class T1 : public T1Base {};
template <typename T> class T2 : public T2Base {};

int main() {
    Base *b = find_by_name();

    if (dynamic_cast<T1Base*>(b))
        cout << "T1" << endl;
    else if (dynamic_cast<T2Base*>(b))
        cout << "T2" << endl;
    else
        cout << "unknown" << endl;

    delete b;

    return 0;
}

Tags:

C++

Rtti

Dynamic