Ok so I have an abstract base class which has a virtual function getTitle() which is also used in all subclasses. The subclasses also have their own unique member functions and variables. I then have a function where I create a dynamic array of the type base class pointers which works fine since all of the subclasses inherit from the base class. My issue is however when I later need to create vectors for each subclass type and push_back() all of the values from each subclass to the corresponding vector. I don't know how to do this since they know are of the type *baseClass.
note start values: size = 0;
BaseClass** array = new BaseClass*[0]
void add(BaseClass* base)
{
size ++;
BaseClass** temp_array = new BaseClass*[size];
for(int i = 0; i < size - 1; i++)
{
temp_array[i] = array[i];
}
array[size - 1] = base;
delete[] array;
array = temp_array;
}
void addSubClass(const SubClass& sub)
{
SubClass* copy = new SubClass(sub);
add(copy);
delete copy;
}
std::vector<SubClass> getSubClassElements() const
{
std::vector<SubClass> vector;
//HERE IS WHERE I NEED HELP
return vector;
}
question from:
https://stackoverflow.com/questions/65851267/how-should-i-define-the-function-getsubclasselements 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…