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);
}