typeindex
#include <type_traits>
struct A {};
struct B {};
int main() {
std::type_index typeA = typeid(A);
std::type_index typeB = typeid(B);
if (typeA == typeB) {
std::cout << "A and B are the same type" << std::endl;
} else {
std::cout << "A and B are different types" << std::endl;
}
}class Animal {
public:
virtual std::type_index getTypeIndex() const = 0;
};
class Dog : public Animal {
public:
std::type_index getTypeIndex() const override {
return typeid(Dog);
}
};
class Cat : public Animal {
public:
std::type_index getTypeIndex() const override {
return typeid(Cat);
}
};
int main() {
Dog dog;
std::type_index dogType = dog.getTypeIndex();
// ...
}