39

For my latest CS homework, I am required to create a class called Movie which holds title, director, year, rating, actors etc.

Then, I am required to read a file which contains a list of this info and store it in a vector of pointers to Movies.

I am not sure what the last line means. Does it mean, I read the file, create multiple Movie objects. Then make a vector of pointers where each element (pointer) points to one of those Movie objects?

Do I just make two vectors - one of pointers and one of Movies and make a one-to-one mapping of the two vectors?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Ayush
  • 41,754
  • 51
  • 164
  • 239

4 Answers4

72

It means something like this:

std::vector<Movie *> movies;

Then you add to the vector as you read lines:

movies.push_back(new Movie(...));

Remember to delete all of the Movie* objects once you are done with the vector.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • +! Yep - that's what I took from question too – Robben_Ford_Fan_boy May 17 '10 at 22:52
  • If I create a vector of ints, I have to push back an int. If it is a vector of bool, I push back a bool. In your statement, do I push back a Movie object or a Movie pointer? – Ayush May 17 '10 at 23:00
  • You are pushing back a pointer to a (newly created) Movie object. – schnaader May 17 '10 at 23:04
  • gotcha. Now, objects that I have added to the created vector: std::vector movies; do I access them normally as movies[0], movies[1] etc ? – Ayush May 17 '10 at 23:20
  • 1
    @xbonez: movies[0] will give you the first **pointer to a movie** in your vector. This might seem confusing, because if you were to allocate the Movie objects dynamically (without vectors, ie as an array), you'd still be using pointers but movies[0] would give you the object directly. In any case, the rule of them with vectors is that when you declare a vector (like `std::vector movies`), whatever's between the <>'s is what you get out when you access an element. So here, you get a Movie * out - which is a pointer to a movie. – Cam May 17 '10 at 23:27
  • oops: *In any case, the rule of **thumb** with vectors... (can no longer edit). – Cam May 17 '10 at 23:34
  • gotcha...thanks. this whole pointers thing is so confusing...ugh – Ayush May 18 '10 at 01:24
  • i have a basic question!, what is the difference between vector* A and vector A ?, when i use the first one it requires to use -> while the other one requires . only – Annunaki Dec 26 '18 at 20:56
  • Hi, what would be the proper way to delete pointer objects from vector. For example, I decided to movies.erase(movies.begin()), does this guarantees the delete of pointer objects? – Coconut Aug 20 '19 at 02:55
15

As far as I understand, you create a Movie class:

class Movie
{
private:
  std::string _title;
  std::string _director;
  int         _year;
  int         _rating;
  std::vector<std::string> actors;
};

and having such class, you create a vector instance:

std::vector<Movie*> movies;

so, you can add any movie to your movies collection. Since you are creating a vector of pointers to movies, do not forget to free the resources allocated by your movie instances OR you could use some smart pointer to deallocate the movies automatically:

std::vector<shared_ptr<Movie>> movies;
ebasconp
  • 1,608
  • 2
  • 17
  • 27
  • 3
    Or even better use `std::vector>` if the vector is the only object which maintain lifetime of `Movie` objects – Maciej S Aug 11 '17 at 09:49
3

By dynamically allocating a Movie object with new Movie(), you get a pointer to the new object. You do not need a second vector for the movies, just store the pointers and you can access them. Like Brian wrote, the vector would be defined as

std::vector<Movie *> movies

But be aware that the vector will not delete your objects afterwards, which will result in a memory leak. It probably doesn't matter for your homework, but normally you should delete all pointers when you don't need them anymore.

mooware
  • 1,722
  • 2
  • 16
  • 25
1

I am not sure what the last line means. Does it mean, I read the file, create multiple Movie objects. Then make a vector of pointers where each element (pointer) points to one of those Movie objects?

I would guess this is what is intended. The intent is probably that you read the data for one movie, allocate an object with new, fill the object in with the data, and then push the address of the data onto the vector (probably not the best design, but most likely what's intended anyway).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • No, I think what's being pushed onto the vector is actually the pointer. ie `movies.push(new Movie()); movies.back()->getData();` or something. – Cam May 17 '10 at 22:57
  • @incrediman: Oops, quite right. My fingers skipped a couple of words as I was typing... – Jerry Coffin May 17 '10 at 23:06