Search code examples
c++pointersc++11netbeansshared-ptr

How do I access the contents of a std::shared_ptr?


Here is my code.

std::shared_ptr<WSUStudent> WSUStudent::registerStudent(
   std::string lastName,
   std::string firstName
)
{
   auto result = std::shared_ptr<WSUStudent>(new WSUStudent(lastName, firstName));

   s_allStudents.insert(&result);

   return result;
}

I have successfully managed to change the function so it returns a shared_ptr instead of a normal pointer. I have successfully encapsulated the 'new' statement with a shared pointer, as per the assignment (I think), but the line of code below 'auto' didn't work without the &, and it doesn't work WITH the &. I receive an error stating that there is no matching function call, with or without the &. That line of code is attempting to insert the new student (or a pointer to the new student?) into the list of all students. However the 'insert' method is not locally overridden, so I'm not quite sure what to do here. Error printed below.

/mnt/hgfs/Data Structures and Algorithms/HW04/WSUStudent.cpp:146:32: error: no matching function for call to ‘std::set<WSUStudent*>::insert(std::shared_ptr<WSUStudent>*)’
    s_allStudents.insert(&result);

The point of this assignment is to fix memory leaks ('new' statements that don't get deleted with their pointers) by turning normal pointers into weak pointers and shared pointers. The original code is as follows.

WSUStudent *WSUStudent::registerStudent(
   std::string lastName,
   std::string firstName
)
{
   auto result = new WSUStudent(lastName, firstName);

   s_allStudents.insert(result);

   return result;
}

Am I going about this wrong? I can't get the s_allStudents line to run.


Solution

  • Given the type of s_allStudents, you can use:

    s_allStudents.insert(result.get());
    

    However, a better option will be to change type of s_allStudents.

    static std::set<std::shared_ptr<WSUStudent>> s_allStudents;
    

    and use:

    s_allStudents.insert(result);
    

    Update

    The default operator<() of shared_ptr is such that the objects in s_allStudents will be sorted by pointer value. If you would like to sort the objects using a different criterion, you'll need to define a custom functor/function as a parameter of the template.

    struct MyCompare
    {
       bool operator<(shared_ptr<WSUStudent> const& lhs,
                      shared_ptr<WSUStudent> const& rhs) const
       {
          // Implement the logic ...
       }
    };
    

    and use it as:

    static std::set<std::shared_ptr<WSUStudent>, MyCompare> s_allStudents;