pavlos163
pavlos163

Reputation: 2890

C++ passing by reference in constructor

So I am making a program in C++ and my main method has to construct an object that takes as a parameter a vector.

This is my code:

int main() {
  vector<Seller> *staff = new vector<Seller>;

  for (int i = 0; i < 50; i++) {
    staff->push_back(Seller(i));
  }

  BookStore store(*staff);

  deque<Book> books;
  books = store.getBooks();
}

So, these are some pretty simple Object-Oriented concepts I think.

My goals are:

First, initializing an empty vector of sellers. A Seller is an object that has a constructor:

Seller(int i);

And represents, of course, a seller.

Then, I want to fill in the vector with actual Sellers. These are constructed in the for loop.

Then, I want to create a Store, which takes as an argument the sellers that work there.

Finally, I create a new deque called books, and I assign to it the value of books in the Store class. The initialisation of the Books deque is done in the constructor of the Store:

Store::Store(vector<Seller> &sellers) {
  this->sellers = sellers;
  this->books = deque<Book> (100, "Harry Potter");
}

So this is the code and I am wondering if I am making a mistake in the passing arguments to new constructors part.

I am a bit confused when passing by reference so I am asking for a bit on help on that part. I have two main questions:

1) Are there any errors there, considering how I want to run my program? Consider also that in the rest of the main method (not included here) I constantly change the value of the books deque. 2) Is there any way to replace an element in a deque without having to erase and insert?

Is there a built-in function replace? If not, is the below code going to work if I just want to replace a value in the deque?

For example, if the deque is like that:

3 4 5 2

And it (an iterator) has value 2.

Then I want the deque to become:

3 4 6 2

When doing:

books.erase(it);
books.insert(it, 6);

Thanks for any tips or help!

Upvotes: 1

Views: 1197

Answers (1)

Alberto M
Alberto M

Reputation: 1077

OK, here a short analysis.

Firstly, the unique real error I found: staff is defined as a pointer and is given a value with new but is never released. You should anyway avoid using raw pointers, so either create the object on the stack:

vector<Seller> staff{};

or use a smart pointer

auto staff = make_unique<vector<Seller>>{};

(you will then have to learn something about the ownership semantics, so as you still are a beginner I'd recommend the first solution).

Then, notice how the line

this->sellers = sellers;

in Store::Store will make a copy of the sellers vector, which probably is not what you meant. If you wanted your store to reference the variable created on main(), you should redefine your Store as

class Store {
   // ...
   vector<Seller>& sellers;
   //...
};

and the constructor as

Store::Store(vector<Seller> &sellers) :
   sellers{sellers}  // reference member variables must be given a value before the body of the constructor begins
{
  books = deque<Book> (100, "Harry Potter");
}

For the same reason, your line

books = store.getBooks();

will make a copy of the deque (but maybe in this case it was intended).

Finally, C++ offers many container manipulating functions under the <algorithms> library. Take a look at the reference. But if you already have an iterator to the element you want to replace, you do not need such algorithms, just write:

*it = 6;

Upvotes: 2

Related Questions