Reputation: 89
i have vector of pointers: std::vector<Customer*> customersList
Now i want to get one of the elements and make my operations on him.
i'm not sure that i know how, my guess was:
Customer* source = restaurant.getCustomer(cust);
the problem is that i don't know if in c++ it will create new object or i will just get a reference to him. There is my getter method:
Customer* Table::getCustomer(int id) {
for (int i = 0; i < customersList.size(); ++i) {
if (customersList[i]->getId() == id)
return customersList[i];
}
return nullptr;
}
Thank you
Upvotes: 3
Views: 501
Reputation: 38315
The member function will return a copy of the pointer, i.e. the Customer
object itself is not copied, only the reference to it. Modifying the returned Customer*
will result in modifications of the pointee (the underlying object).
Note that you also want to use the <algorithm>
header, specifically std::find_if
.
const auto customer = std::find_if(customerList.begin(), customerList.end(),
[id](const Customer* c){ return c->getId() == id; });
return customer == customerList.cend() ? nullptr : *customer;
Upvotes: 5