Lets say I have the following
class Parent {
protected:
virtual void overrideMe() = 0;
}
class ChildA : public Parent {
protected:
void overrideMe() = 0{}
}
class ChildB : public Parent {
protected:
void overrideMe() = 0{}
}
class UtilClass {
public:
vector<Parent> planes;
void compute() {
Parent& p = planes[0];
}
}
In this case I’d get in the compute() in UtilsClass an error, that “Parent” cannot be initialized.
What I’d like to do is to fill the array “planes” (with either ChildA or childB, i.e. non mixed type) in UtilClass and do some calculations.
Do I have to use pointers during initialization, or better to use to use template? I’m almost sure that template usage is not necessary as I’d like to limit the usage to only children of Parent class.
vector<Parent> planes;doesn’t make sense because aParentcan’t exist. It’s an abstract class. You can have a vector of pointers or, better yet, smart pointers.Even if
Parentwasn’t abstract, the vector ofParentobjects would suffer from object slicing and it’s probably not what you want, since it would break polymorphism.