There is some great opportunity to learn here, so I hope that you don't mind me straying a bit from your question. First of all you should get in the habit of never working with owning raw pointers, i.e. don't allocate an object and hold it in a raw pointer. A raw pointer is for instance the variable sendback in your function which is of type ClassObject * . By "owning" I mean the fact that you are responsible for deleting the memory yourself.
Instead, you should always keep the ownership of the object in a smart pointer if it needs to be dynamically allocated. I.e. std::unique_ptr or std::shared_ptr with preference for unique_ptr unless you really need shared ownership. If you don't have a C++11 compatible compiler you can use the Boost equivalents boost::scoped_ptr and boost::shared_ptr. So whenever you want to dynamically allocate an object, always place it in a smart pointer - this will ensure that the object is destroyed when the smart pointer goes out of scope. Now if you want to return a dynamically created object, return a smart pointer that holds the object. As a rule of thumb, if you need to write delete in your code, you probably should have used a smart pointer.
For instance, to create a new dynamically created ClassObject, you would write:
std::unique_ptr<ClassObject> myObj = std::unique_ptr<ClassObject>(new ClassObject());
or even better, use auto to avoid having to write the type explicitely:
auto myObj = std::unique_ptr<ClassObject>(new ClassObject());
with c++14 that should become:
auto myObj = std::make_unique<ClassObject>();
Using a smart pointer prevents you from running into some potential problems with making sure that these objects get deleted in the face of exceptions.
In your case however, you don't really need to return an object at all. If you want to indicate that there was no object found, then return NULL (or really you should use the C++11 nullptr if it is available with your compiler). Another option is to return the iterator to the object you found or data.end() if you couldn't find it.
You also need to be very careful when using your function, since the user of the function does not have any guarantee that the object that you return a pointer to isn't destroyed by some subsequent operation - but this may be more of a documentation issue.
Another point to make might be to take the argument to the function as a const string & instead of just a string to avoid a temporary copy of the parameter (that might happen).
There is also some more discussion on a question remarkably similar to your use case if you take a look at Herb Sutter's blog:
http://herbsutter.com/2013/05/13/gotw-2-solution-temporary-objects/
as well as some further discussion on this subject at:
http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/
Herb Sutter has written a number of excellent books on C++ and is taking active part in the further development of the C++ language, so don't take my word for this, take his... :).
Cheers!
deleteoperator to get rid of objects allocated withnew.newit won't go "out of scope" and won't be deallocated for you automatically. You must deallocate it usingdelete. (Pedantry: it is possible that some classes are coded so as to allow the object to automaticallydeleteitself - or be deallocated without you explicitly callingdeletebut this requires extra code by the programmer and isn't normally a consideration).auto_ptris obsolete and should not be used any more.