Checking the object type in C++11
Asked Answered
L

1

6

I have class B that inherits from A.

class A
{
};

class B : public A
{
};

And I have three objects.

A* a = new A();
A* a2 = new B();
B* b = new B();

I'd like to if check a is object of type A, a2 is object of type B (not A), and b is object of type B.

I tried typed comparison, but it doesn't give me correct answer.

cout << (typeid(*a) == typeid(A)) << endl; // -> 1
cout << (typeid(*a2) == typeid(A)) << endl; // -> 1
cout << (typeid(*b) == typeid(A)) << endl; // -> 0

cout << (typeid(*a) == typeid(B)) << endl; // -> 0
cout << (typeid(*a2) == typeid(B)) << endl; // -> 0
cout << (typeid(*b) == typeid(B)) << endl; // -> 1

I tried dynamic casting, but I got compile error.

B* derived = dynamic_cast<B*>(a);
if (derived) {
    cout << "a is B";
}
derived = dynamic_cast<B*>(a2);
if (derived) {
    cout << "a2 is B";
}
derived = dynamic_cast<B*>(b);
if (derived) {
    cout << "b is B";
}

typename.cpp: In function 'int main(int, char**)':
typename.cpp:27:36: error: cannot dynamic_cast 'a' (of type 'class A*') to type 'class B*' (source type is not polymorphic)
     B* derived = dynamic_cast<B*>(a);
                                    ^
typename.cpp:31:34: error: cannot dynamic_cast 'a2' (of type 'class A*') to type 'class B*' (source type is not polymorphic)
     derived = dynamic_cast<B*>(a2);

I used static casting, but I got the answer wrong.

B* derived = static_cast<B*>(a);
if (derived) {
    cout << "a is B"; // -> YES
}
derived = static_cast<B*>(a2);
if (derived) {
    cout << "a2 is B"; // -> YES
}
derived = dynamic_cast<B*>(b);
if (derived) {
    cout << "b is B"; // -> YES
}

How can I correctly identify the object type in C++11?

Lettie answered 20/6, 2013 at 2:43 Comment(3)
*a is of type A and *b is of type B per their declaration.Isochroous
Dynamic cast will work only with virtual but your code doesn't have any virtual function.Aniseikonia
Best minimal solution similar to your code: coliru.stacked-crooked.com/…Steverson
D
11

Some classes are polymorphic, some are non-polymorphic.

A polymorphic class has one or more virtual functions (possibly inherited), a non-polymorphic class has zero virtual functions.

Your A and B are non-polymorphic.

A polymorphic version of A and B will exhibit the behaviour you want:

#include <iostream>
#include <typeinfo>

using namespace std;

struct A
{
    virtual ~A() {}; // add virtual function
};

class B : public A
{
};

A* a = new A();
A* a2 = new B();
B* b = new B();

int main()
{
    cout << (typeid(*a) == typeid(A)) << endl; // -> 1
    cout << (typeid(*a2) == typeid(A)) << endl; // -> 0 <-- CHANGED
    cout << (typeid(*b) == typeid(A)) << endl; // -> 0

    cout << (typeid(*a) == typeid(B)) << endl; // -> 0
    cout << (typeid(*a2) == typeid(B)) << endl; // -> 1 <-- CHANGED
    cout << (typeid(*b) == typeid(B)) << endl; // -> 1
}

Instances of a polymorphic class store the dynamic type of their most derived object at runtime.

(In your example a2 is of type pointer-to-A, and is pointing at an object of type A, however this object is only a base class subobject of the most dervived object of type B. What you want to get is the type of this most derived object B when querying a2. For this you need a polymorphic class.)

That is how polymorphic classes support dynamic_cast and typeid of the most derived object (as well as virtual function dispatch).

Non-polymorphic classes do not have this information, so they can only report the static type known at compile-time. Non-polymorphic classes are more compact and efficient then polymorphic classes. That is why not all C++ classes are polymorphic. The language leaves it up to the programmer to chose the tradeoff between performance and functionality. For example:

struct X { int x; };
struct Y : X {};
struct Z : Y {};

On my system non-polymorphic Z is sizeof(Z) == 4 bytes, same as an int.

struct X { int x; virtual ~X() {}; };
struct Y : X {};
struct Z : Y {};

Now after making Z polymorphic, sizeof(Z) == 16 bytes. So an array of Z is now 300% larger, because each Z instance has to store its type information at runtime.

Davila answered 20/6, 2013 at 2:59 Comment(6)
#17222168Lettie
@Sharth: No, that's not true; you don't get it in either case.Shouldst
@LightnessRacesinOrbit, @prosseek: Thank you for the correction!. I had thought that was one of the main reasons for the = default case.Hollis
@sharth: That would have been nice but alas not.Shouldst
The comments are about a now-deleted incorrect statement that using = default instead of {} will inhibit default move constructor generation.Davila
Note of course that you can get default move constructor generation back with A(A&&) = default;Dextroamphetamine

© 2022 - 2024 — McMap. All rights reserved.