Hello for some reason I'm having a hard time understanding smart pointers, specifically shared_ptr in this case. Could someone please tell me what i'm doing wrong in this program. I have a strong feeling i'm messing up the memory management through copying and assigning
Edit: Sorry it works now thank you. Forgot to change the prototypes. Anyways although this really isn't necessary I'm using this for an exercise in my c++ book, C++ Primer Plus. Here's the Exercise if your interested and seeing why I needed this.
Modify Listing 16.9 (vect3.cpp) as follows:
Add a price member to the Review structure.
Instead of using a vector of Review objects to hold the input, use a vector of shared_ptr<Review> objects. Remember that a shared_ptr has to be initialized with a pointer returned by new.
Follow the input stage with a loop that allows the user the following options for displaying books: in original order, in alphabetical order, in order of increasing ratings, in order of decreasing ratings, in order of increasing price, in order of decreasing price, and quitting.
Here’s one possible approach. After getting the initial input, create another vector of shared_ptrs initialized to the original array. Define an operator<() function that compares pointed-to structures and use it to sort the second vector so that the shared_ptrs are in the order of the book names stored in the pointed-to objects. Repeat the process to get vectors of shared_ptrs sorted by rating and by price. Note that rbegin() and rend() save you the trouble of also creating vectors of reversed order.