I want to create an array of objects. Current I'm using std::string a[10] kind of syntax, but I'm not sure if it's the best practice to do so.
First, does std::string a[10] actually call the constructor and allocate memory for 10 strings? I think std::string a declares a but doesn't bind an object to it (I might be wrong). What about std::string a[10]?
Second, after ``declaring'' the array via std::string a[10](if I'm not mistaken about the declaration), how to initialize the elements? See below for my confusion.
This gives out error:
std::mutex a[100];
for (int i = 0; i<100; i++)
{
a[i] = std::mutex(); // error;
}
whereas this is fine:
std::thread a[100];
for (int i = 0; i<100; i++)
{
a[i] = std::thread(func, NULL);
}
It's confusing to me why these two snippets give different results. Is the second snippet just copying objects to already created a[i]? If so, I might be doing things wrong..
T a[100]; a[0] = new T();makes 100 strings and then makes a balloon and attaches the first string to the balloon. In C++,T a[100]; a[0] = T();makes 100 ballons, then makes another balloon, pops the first of the 100 and puts the new balloon in its place. You can't pop mutexes.