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
160 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 500 total points
Comment Utility
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
Comment Utility
Thank-you old school!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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

9 Experts available now in Live!

Get 1:1 Help Now