Есть 1 базовый класс и 2 класса наследника.
#include <iostream>
#include <vector>
#include <cstdio>
#include <typeinfo>
#include <map>
using namespace std;
class NotBase1;
class NotBase2;
class Base;
class Base{
public:
int a=0;
template<typename T>
T* get_type();
};
class NotBase1:public Base{
public:
NotBase1* get_type(){
return this;
}
int z =90;
};
class NotBase2:public Base{
public :
NotBase2* get_type(){
return this;
}
int p = 100;
};
int main() {
std::vector<Base*> stru;
NotBase1* b1 = new NotBase1;
NotBase2* b2 = new NotBase2;
stru.push_back(b1);
stru.push_back(b2);
stru[0]->get_type<NotBase1>()->z;
return 0;
}
Каким образом и можно ли, при обращении к элементу вектора, работать с его элементами как с дочерними объектами класса? Не используя шаблоны и не указывая явно какого типа объект как тут stru[0]->get_type()->z;