Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-02-09
1
Medium Priority
?
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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…

722 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