I have came from those questions:
- Why can't we declare a std::vector<AbstractClass>?
- abstract classes in std containers
- How to store a vector of objects of an abstract class which are given by std::unique_ptr?
They all suggested that I should use pointer or smart pointers instead.
As far as I know, Data are dynamically allocated in std::vector
which means that there are pointers internally in the std::vector
. So why I can not use abstract classes directly? why I have to use pointers(The one I specified) for pointers(the internally) in order to use abstract classes with std::vector
. I know some features like std::vector::resize
won't work. However, std::vector::reserve
and std::back_inserter
will solve the problem.