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

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
?
223 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 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

Technology Partners: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

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