How to verify inherited type

109 Views Asked by At

I'm working in an old code where a pointer of an object of type A is passed to a function:

void fun(A* a)
{
    if (dynamic_cast<const B*>(a) != NULL)
    {
    // Use B object
    }
    else
    {
    // Use C object
    }

}

The classes B and C inherit from A and they kind of used dynamic_cast to test the input type (actually to test if "castable"). Which seems wrong.

I tried using std::is_same but I may be doing something wrong:

    if(std::is_same<decltype(*a), A>::value ) {
        std::cout << "Equal" << std::endl;
    }
    else
    {
        std::cout << "Not equal" << std::endl;
        std::cout << typeid(*a).name() << std::endl; // This
        std::cout << typeid(A).name() << std::endl;  // And this prints the same
    }

I always get into the "Not equal" case event if the following line print the same.

  1. Am I doing something wrong with std::is_same ?
  2. Is there another (better) way?
0

There are 0 best solutions below