Solved

How do I return a smart pointer from a function call in C++?

Posted on 2015-02-09
1
145 Views
Last Modified: 2015-02-23
Hi:

I have a MS C++ (unmanaged) application and am new to smart pointer usage.
We've decided to go with the smart pointer implementation that is part of C++ 11.
I haven't really used smart pointers very much. I've checked out the wiki definition and
have a basic idea.

I have a question regarding returning smart pointers from a function/method call.

Consider the following example using raw pointers:
FOO * CreateFooObject()
{
   FOO * foo = new FOO();
   return foo;
}

Open in new window

What would the best approach be for converting this to using smart pointers, to use a shared pointer?
I thought that this would be the appropriate pattern, but I wanted to confirm.
shared_ptr<FOO> CreateFooObject()
{
   shared_ptr<FOO>  foo = make_shared<FOO>();
   return foo;
}

Open in new window


Thanks,
JohnB
0
Comment
Question by:jxbma
1 Comment
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40600069
unique_ptr - Allows exactly one owner of the underlying pointer. Use as the default choice for POCO unless you know for certain that you require a shared_ptr.
shared_ptr  - Reference-counted smart pointer. Use when you want to assign one raw pointer to multiple owners
weak_ptr - Special-case smart pointer for use in conjunction with shared_ptr. A weak_ptr provides access to an object that is owned by one or more shared_ptr instances, but does not participate in reference counting.
https://msdn.microsoft.com/en-us/library/vstudio/hh279674(v=vs.110).aspx

Given this the question here is, what will you be doing with the pointer once it is created?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question