any
#include <typeinfo>
struct Base {
virtual std::string type() const = 0;
};
struct Derived1 : Base {
std::string type() const override { return "Derived1"; }
};
struct Derived2 : Base {
std::string type() const override { return "Derived2"; }
};
int main() {
std::vector<Base*> objects;
objects.push_back(new Derived1());
objects.push_back(new Derived2());
for (auto& obj : objects) {
std::cout << obj->type() << std::endl;
}
return 0;
}Last updated