Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ Vector of Objects, are they all named temp?

New to C++ OOP, I recently learned about classes and objects. I created a straightforward class and menu-driven program that adds a temp object to a vector of movies. I have a quick question that I can’t quite understand.

Am I just pushing multiple "temp" objects into the vector?

In my head i’m visualizing this as vector my_movies = {temp, temp, temp}; and continously adding ‘temp’ objects until the user is done. Is this the right way to picture it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <iostream>
#include <vector>

using namespace std;

class Movie
{
  private:
    string name;
  public:
    string get_name() { return name; }
    void set_name(string n) { name = n; }
};

void menu() {
  cout << "1. Add movie" << endl;
  cout << "2. Show Movies" << endl;
  cout << "3. Quit" << endl;
}

int getChoice(int &choice) {
  cout << "Enter you choice: ";
  cin >> choice;
  return choice;
}

int main() {

  vector<Movie> my_movies;
  int choice = 0;
  string name;

  do {
    menu();
    getChoice(choice);
    switch (choice) {
      case 1: {
        Movie temp;
        cout << "Set user name: ";
        cin >> name;
        temp.set_name(name);
        my_movies.push_back(temp);
        break;
      }
      case 2: {
        for (auto &mv : my_movies)
          cout << mv << endl;
        break;
      }
    }

  } while (choice != 3);

  return 0;
}

>Solution :

In your case, when you are calling push_back it will copy your "temp" object, which is a local object on the stack. It will be copied into a new object which is stored on the heap, held by the vector object. The vector will store these as an array internally (the default vector with the default allocator etc).

It’s also possible to "move" the object (under C++11 and later), if you understand the difference, but doing push_back(std::move(temp)), which generally gives better performance. In your case it would avoid copying the string member "name", and move it instead, avoiding a new allocation for the string inside the Movie in the vector.

See here for more details on push_back

Appends the given element value to the end of the container.

  1. The new element is initialized as a copy of value.

https://en.cppreference.com/w/cpp/container/vector/push_back

If you are just talking about the name of the movie, it will be what ever is entered from cin. Objects don’t have names themselves. The local variable name "temp" is just what you see when you write the code, but is just used to tell the compiler which object is being used – the object itself doesn’t have a name form the compilers perspective.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading