Solved

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

Posted on 2015-02-09
1
127 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now