If I have a base class and a derived class, I can create a vector of base class pointers, if I want to group multiple base and/or derived classes together in a container.
Example:
class base
{
}
class derived : public base
{
}
std::vector<base*> group;
But is it possible to do the following?
std::vector<base> group;
ie: Without the pointers, which require newing and deleteing?
Edit: In 2015 I didn't know about polymorphism, if you are reading this question in 2022 it would be worth searching for some information about that.
To give a bit of color on this, a std::vector is a template class which takes a template parameter T. You may think of it as
template<typename T>
std::vector<T>
All this means is that a std::vector contains elements of the same type. T describes the type. C++ is statically typed, meaning that all the types are specified at compile time.
T can be one of many different types. However, each instance of a std::vector contains elements of the same type.
This is in part because the compiler needs to know how large T is. In other words, what size does it occupy in memory? This is a fairly obvious constraint. You cannot pack elements into memory unless the size of the elements are known. Further, you cannot jump to an element within an array in O(1) time, unless the index of the element can be converted into a memory address using a multiplication.
When it comes to pointers, all pointers are the same size. Therefore you can create a std::vector<base*>. However, a class base may not have the same size as class derived, therefore you cannot create a std::vector<base or derived>.
As others have explained in the answers, a std::vector<base> will not behave polymorphically.