Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Consider the following code. What I am trying to do is initialize an array of pointers from a supplied array of values. I'm trying to figure out the best way to copy those values into the pointers, and do it in a C++11 sort of way. I'm using VS2010 to compile this code.

The part that really bugs me is having to use the external "i" to get at the correct index into the array of pointers. I considered using std::array for this but I was having a lot of trouble using the std::algorithms like for_each and getting it to compile under VS2010. That may have just been user error.

I am open to using std::array if someone can show me correctly compiling syntax for VS2010. I haven't had a lot of luck finding good examples of std::array on stackoverflow or elsewhere. I'm also not entirely sure it will fix the problem of the external "i" either.

template <int ArraySize>
class testWithArray
{
   public:
   testWithArray(int* myarray)
   {            
      std::for_each(ArrayOfPointers_, ArrayOfPointers_ + ArraySize, [] (int* arrayElem) 
      {
         arrayElem = new int (0);
      });

      int i = 0;
      std::for_each(myarray, myarray + ArraySize, [&] (int arrayElem)
      {
         *(ArrayOfPointers_[i++]) = arrayElem;
      });
   }
   private:
   int* ArrayOfPointers_[ArraySize];
};



void main()
{
   int plainArray[5] = {0,1,2,3,4};
   testWithArray<5>  myArrayClass(plainArray);
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

I'm really not sure why you are using an array of pointers to integers rather than just an array of integers (which is what I would strongly recommend), but if that is really what you need and you are aware of what you are doing (including the fact that your program is currently leaking memory), then you could use std::transform() to rewrite your constructor this way:

#include <algorithm>

// ...

testWithArray(int* myarray)
{
    std::transform(myarray, myarray + ArraySize, ArrayOfPointers_,
        [] (int arrayElem)
    {
        return new int(arrayElem);
    });
}

But again, consider whether you really need to have an array of pointers. And if you really have to, then consider using smart pointers instead of raw pointers (pick the one that models the most appropriate ownership policy for your application).

share|improve this answer
    
The default capture specification "[&]" is unnecessary - this lambda captures nothing. –  Casey Jun 10 '13 at 19:38
    
@Casey: Correct, it was a left-over of a copy-paste. Edited, thank you –  Andy Prowl Jun 10 '13 at 19:38
    
Thanks that's what I needed to know! –  obermeister Jun 10 '13 at 19:41
    
@obermeister: Glad it helped! –  Andy Prowl Jun 10 '13 at 19:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.