tags:

views:

190

answers:

4

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?

+8  A: 

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
+! Yep - that's what I took from question too
David Relihan
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?
xbonez
You are pushing back a pointer to a (newly created) Movie object.
schnaader
gotcha. Now, objects that I have added to the created vector: std::vector<Movie *> movies;do I access them normally as movies[0], movies[1] etc ?
xbonez
@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<Movie *> 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
oops: *In any case, the rule of **thumb** with vectors... (can no longer edit).
Cam
gotcha...thanks. this whole pointers thing is so confusing...ugh
xbonez
+1  A: 

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
@Jerry: 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
@incrediman: Oops, quite right. My fingers skipped a couple of words as I was typing...
Jerry Coffin
+3  A: 

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.

ebasconp
A: 

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
"It probably doesn't matter for your homework" - I'm not so sure - it sounds like the prof is setting him up for this!!!
David Relihan