vector of classes: what is the '*'?

In theory a simple question:

Normally when declaring a vector of classes in c++, I do something like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdlib>
#include <iostream>

#include <vector>

using namespace std;

class Class01
{
     public:
          int int01;
};

int main()
{
     vector<Class01 *> class01_vector;

     //...
}


So when declaring class01_vector in main(), I always have that '*' in between the <>'s, after the type: vector<Class01 *>. Does that '*' need to be there? What does it do? I've just always put it there because I was taught to, I've never really understood why. Thanks to all who clear this up for me, seems like it could be a pretty simple answer.
It means that what you're storing are pointers, rather actual instances of the class. You should have covered pointers pretty much at the beginning of your course, as they are a pretty fundamental concept in C++ (although you're usually trying to avoid using them too much, writing useful programs completely without the use of pointers becomes, at some point, pretty much impossible).
It means that the vector contains pointers to Class01, without the * then the vector would just contain Class01 objects. It doesn't "need" to be there, but it can be, it depends on what you need to do.
It makes sense if you intend to store object of derived classes in that container.
Or if the container does not have the ownership
Topic archived. No new replies allowed.