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
198 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
[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
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
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 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…

752 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