Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-02-09
1
Medium Priority
?
178 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:
Michael Fowler earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

885 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