Solved

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

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is AWS.RDS considered a part of "Big Data"? 4 111
Build VS2015 project in bat command 3 88
c++ syntax question 9 49
Safe conversion? 4 69
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

823 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