mardi 2 février 2021

C++ Vector of abstract class of specific base class, as another class member

I have:

  1. A base abstract class Shape.
  2. Then, several child classes, one of them, Rectangle.
  3. A template class called GeneralObject<T>, having a private member T shape (T has a static assert check for being child of Shape), in order for a GeneralObject to be created: GeneralObject<Rectangle>
  4. An ObjectManager which I want to include a vector of GeneralObject(s).

How should I implement this functionality in order to have this vector of Template classes with arbitrary shapes?

I suppose that one way is to remove the static assert from 3) and create something like:

std::vector<GeneralObject <std::shared_ptr<Shape>>> objects;

but I am not sure if I am following the proper approach. It would be great if I could also assert that the template function uses a child of Shape.

Aucun commentaire:

Enregistrer un commentaire