Order of creation of objects in arrays

K

kalki70

Hello,

I've been loooking for info about this issue, but I still can't find.
If I create an array of objects, are the constructors called in some
predefined order, or it is compiler-dependent?

For instance, having a class "A":

A a[10];

What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.

Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.

Thanks a lot,

Luis
 
X

xmli1976

I think you can test it by this way:
Declare a global variable which is initialize to 0,for example,int i=0;
In the Constructor ,you can asign i to a class member variable,for
example,num=i++;
And then you can print the num variables of objects in the array,then
you can see which one is first constructed.


kalki70 写é“:
 
X

xmli1976

#include <stdio.h>
int g=0;
class mycls
{
public:
int i;
mycls()
{
i=g++;
}
};
int main()
{
mycls myarray[10];
printf("array construct order:");
for(int j=0;j<10;j++)
printf("%d",myarray[j].i);
return 0;
}
(e-mail address removed) 写é“:
I think you can test it by this way:
Declare a global variable which is initialize to 0,for example,int i=0;
In the Constructor ,you can asign i to a class member variable,for
example,num=i++;
And then you can print the num variables of objects in the array,then
you can see which one is first constructed.


kalki70 写é“:
Hello,

I've been loooking for info about this issue, but I still can't find.
If I create an array of objects, are the constructors called in some
predefined order, or it is compiler-dependent?

For instance, having a class "A":

A a[10];

What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.

Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.

Thanks a lot,

Luis
 
T

Todd Gardner

kalki70 said:
For instance, having a class "A":

A a[10];

What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.

Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.

From the C++ standard

12.6.3
When an array of class objects is initialized (either explicity or
implicity), the constructor shall be called for each element of the
array, following the subscript order; see 8.3.4 [Note: destructors for
the array elements are called in reverse order of their constructions]

Second:

A a[10];

Calls the default constructor on each of on all of the array elements
in order. For a simple test program, demonstrating the order and which
constructor is called:

#include <iostream>

class testing {
public:
testing() {
std::cout << "Default Constructed at " << this << std::endl;
}

~testing() {
std::cout << "Destructed at " << this << std::endl;
}
};

int main()
{
testing mytests[10];

return 0;
}
 
K

kalki70

(e-mail address removed) ha escrito:
I think you can test it by this way:
Declare a global variable which is initialize to 0,for example,int i=0;
In the Constructor ,you can asign i to a class member variable,for
example,num=i++;
And then you can print the num variables of objects in the array,then
you can see which one is first constructed.

Thanks, but I DON'T want to test it. Doing so may just show me a
specific compiler implementation and there can be multiple
implementations.

Luis
kalki70 写é“:
Hello,

I've been loooking for info about this issue, but I still can't find.
If I create an array of objects, are the constructors called in some
predefined order, or it is compiler-dependent?

For instance, having a class "A":

A a[10];

What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.

Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.

Thanks a lot,

Luis
 
K

kalki70

Todd Gardner ha escrito:
kalki70 said:
For instance, having a class "A":

A a[10];

What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.

Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.

From the C++ standard

12.6.3
When an array of class objects is initialized (either explicity or
implicity), the constructor shall be called for each element of the
array, following the subscript order; see 8.3.4 [Note: destructors for
the array elements are called in reverse order of their constructions]

That's the answer I needed!!! Thanks a lot!!!

Best regards,

Luis
Second:

A a[10];

Calls the default constructor on each of on all of the array elements
in order. For a simple test program, demonstrating the order and which
constructor is called:

#include <iostream>

class testing {
public:
testing() {
std::cout << "Default Constructed at " << this << std::endl;
}

~testing() {
std::cout << "Destructed at " << this << std::endl;
}
};

int main()
{
testing mytests[10];

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top