Why is canvas_index not being assigned properly in the initiation list
unless I do an assignment inside the backets
/*
*
=====================================================================================
*
* Filename: test.cpp
*
* Description: Threading Experiment
*
* Version: 1.0
* Created: 12/18/2016 12:46:51 PM
* Revision: none
* Compiler: gcc
*
* Author: Ruben Safir (mn),
ru...@mrbrklyn.com
* Company: NYLXS Inc
*
*
=====================================================================================
*/
#include <iostream>
#include <thread>
namespace testing{
std::thread t[10];
class PIC
{
public:
PIC():beg{&source[0]}, end{&source[99]} , canvas_index
{canvas}
{
canvas_index = canvas;
for(int i = 0; i < 10;i++)
{
t[i] = std::thread([this]{ readin(beg); }
);
std::cout << i << ": Making a thread" <<
std::endl;
beg += 10;
}
};
~PIC()
{
std::cout << "In the destructor" << std::endl;
for(int i=0; i<10; i++)
{
t[i].join();
std::cout << i << ": Joining a thread" <<
std::endl;
}
};
void readin(char * start)
{
for( int i = 0; i<10; i++ )
{
*canvas_index = *start;
std::cout << i << ": Copy " <<
reinterpret_cast<char>(*start) << std::endl;
std::cout << i << ": Copied to
canvas_index " << reinterpret_cast<char>(*canvas_index) << std::endl;
canvas_index++;
start++;
}
};
char * get_canvas()
{
return canvas;
}
private:
char * beg;
char * end;
char * canvas_index;
char * canvas = new char[100];
char source[100] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'
};
//int index;
};
}//end namespace
int main(int argc, char** argv)
{
testing::PIC fido;
for(int i = 0; i<100;i++)
{
std::cout << i << " Canvas Position " << fido.get_canvas()
[i] << std::endl;
}
return 0;
}