0

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..

2
  • @Brian Yeah I got the basic wrong.. Commented Feb 16, 2016 at 22:42
  • In Java, 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. Commented Feb 16, 2016 at 23:27

2 Answers 2

2

I might be wrong

You are definitely wrong. You are mistaking C++ for some other language, probably Java or C#.

In C++, when you say std::string s;, you have a std::string which is fully initialized and constructed. This holds true for array types as well.

Also, pro-tip: drop C arrays and use std::array instead, it's much superior.

As for the difference between your examples, they're both bad practice but the first is illegal mostly because mutex is a special flower which is immovable, which is very rare but technically legal. For the infinite majority of types (that have default constructors in the first place), what you've done is bad practice but should be permissible.

If you want to construct objects in a loop like this, you should probably be looking at std::vector<T> and then use push_back in the loop, as this avoids unnecessary default construction requirements and random MAX_SIZE constants.

Sign up to request clarification or add additional context in comments.

Comments

1

First, does std::string a[10] actually call the constructor and allocate memory for 10 strings?

Yes. Use array of pointers or in-place allocation (on stack) if you want to initialise instances manually.

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.

Array elements are initialised by a no-args constructor. In your code you assign a different value to elements. Some objects allow this, some don't.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.