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

x
?
Solved

How do I iterate over a vector that is wrapped in a smart pointer in Microsoft C++ 11?

Posted on 2015-02-10
2
Medium Priority
?
238 Views
Last Modified: 2015-02-10
Hi:

I have a Microsoft C++ 11 application.

I have a vector of shared_ptrs that get returned from a method call.
In order for that vector not to be leaked, I have wrapped it in a shared_ptr as well.

How do I iterate over that vector when it is wrapped in a smart (shared) pointer?

// Definition of the method
shared_ptr<vector<shared_ptr<Foo>>> GetVectorOfValues(){}

// Now in another method....
{
   ...
   shared_ptr<vector<shared_ptr<Foo>>> values =  GetVectorOfValues();

   for (auto value : values.get())
   {
   }
}

Open in new window


I would have thought that "get()" would get me to the wrapped vector.
How would I go about doing this?

Is there a better pattern to use to achieve my goals?

Thanks,
JohnB
0
Comment
Question by:jxbma
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 40601615
Let me try to 'unwrap' that the old-school way for readability:

// Definition of the method
shared_ptr<vector<shared_ptr<Foo>>> GetVectorOfValues(){}

// Now in another method....
{
   ...
   shared_ptr<vector<shared_ptr<Foo>>> values =  GetVectorOfValues();

   vector<shared_ptr<Foo>>* vec = values.get())
    for (vector<shared_ptr<Foo>>::iterator i = vec->begin(); i != vec->end(); ++i)
   {
        Foo* foo = i->get();
   }
}
                                  

Open in new window


Is that what you had in mind?
0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 40602257
Thank-you old school!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

926 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