Array of object

Jul 2, 2012 at 5:24pm
Let's say we have a class called Test...

 
Test test[10];


Book I'm reading says that this will invoke the constructor for 10 objects of type Test, even though no objects were explicitly created here. Is this true? If so, why? The author doesn't explain this at all.
Jul 2, 2012 at 5:37pm
closed account (zb0S216C)
For each object within the array, 1 call is made to the object's default constructor. The objects were in fact explicitly created - the whole statement is explicit but the creation of the objects appear implicit. If you want to be more explicit, you can use this initialiser list:

1
2
Test test_[3]; // 3 implicitly constructed objects.
Test test_[3] = {(...), (...), (...)}; // 3 explicitly constructed objects. 

Wazzak
Last edited on Jul 2, 2012 at 5:42pm
Jul 2, 2012 at 5:37pm
Every time object is created constructor is called.So constructor is called 10 times.

When constructor is not present, default constructor is provided by compiler.
Jul 2, 2012 at 5:42pm

@ResidentBiscuit

Let's say we have a class called Test...

Test test[10];

Book I'm reading says that this will invoke the constructor for 10 objects of type Test, even though no objects were explicitly created here.


You are wrong. Here ten objects are explicitly created.
Jul 2, 2012 at 5:46pm
Alright so I guess my idea of an array is off. In my mind, Test test[10] sets aside space in memory for 10 consecutive Test objects, and the objects are created at a later time during the program (ie, whenever they get explicitly created)

1
2
3
4
5
Test test[10]; //I was thinking nothing gets created here, just memory reserved.

Test testObj1();     //And this is where the object is actually created.
test[0] = testObj1; 
...


Sounds as if I'm wrong here though.
Jul 2, 2012 at 5:54pm
closed account (zb0S216C)
The objects are guaranteed to be fully constructed by the time the end of the statement is reached (at the semi-colon). The compiler does in fact reserve the memory for those objects. For instance, if no objects were constructed, then this would be invalid:

1
2
3
4
5
6
7
8
9
10
struct X
{
    X(int ia_ = 0, int ib_ = 0) : a_(ia_), b_(ib_) { }
    int a_, b_;
};

int main()
{
    X xs_[2] = {(0, 1), xs_[0]}; // Default constructor call followed by a copy-constructor call.
}

...but it's not.

ResidentBiscuit wrote:
"In my mind, Test test[10] sets aside space in memory for 10 consecutive Test objects, and the objects are created at a later time during the program (ie, whenever they get explicitly created)"

What you're thinking of is placement new:

1
2
3
X *xs_(static_cast<X*>(operator new [] (2 * sizeof(X)))); // No objects built here.

new(xs_) X(0, 1); // 1 object is now available inside xs_. 

Fixed some errors :)

Wazzak
Last edited on Jul 2, 2012 at 5:59pm
Jul 2, 2012 at 6:04pm
@Framework
You must really hate operator=
Jul 2, 2012 at 6:06pm
closed account (zb0S216C)
@naraku9333: Haha :) It's not that I hate the assignment operator, I just prefer functional notation since it looks cleaner.

Wazzak
Jul 2, 2012 at 7:52pm
Ok, so I create an array of objects, and immediately it's filled with the corresponding object? This still seems strange to me
Jul 2, 2012 at 8:07pm
Yes, that's why this works (over simplified example)
1
2
3
int x[10];
for(int i ...)
x[i] = 5;

if it isn't constructed you cant assign. Your example test[0] = testObj1; should be an error if Test doesn't have an operator= overload (and probably copy ctor)..
Last edited on Jul 2, 2012 at 8:08pm
Jul 2, 2012 at 8:15pm
Ooh wow that makes sense now. So if I need a large group of object created at once, with default values, I can just make an array (or any container presumably) of the given type and then they are ready to be used?
Jul 2, 2012 at 8:23pm
I can't speak for all containers, but if you create a vector with a set size it constructs the objects, and it also does if you resize it.
Jul 2, 2012 at 8:32pm
Hmm I'll have to look into what vector does in this situation.

I've learned a lot about arrays here, though. I had no idea before that an array populated itself at creation.
Topic archived. No new replies allowed.