Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Order of creation of objects in arrays

Reply
Thread Tools

Order of creation of objects in arrays

 
 
kalki70
Guest
Posts: n/a
 
      09-04-2006
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

 
Reply With Quote
 
 
 
 
xmli1976@gmail.com
Guest
Posts: n/a
 
      09-04-2006
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


 
Reply With Quote
 
 
 
 
xmli1976@gmail.com
Guest
Posts: n/a
 
      09-04-2006
#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;
}
http://www.velocityreviews.com/forums/(E-Mail 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


 
Reply With Quote
 
Todd Gardner
Guest
Posts: n/a
 
      09-04-2006

kalki70 wrote:
> 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;
}

 
Reply With Quote
 
kalki70
Guest
Posts: n/a
 
      09-13-2006

(E-Mail 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


 
Reply With Quote
 
kalki70
Guest
Posts: n/a
 
      09-13-2006

Todd Gardner ha escrito:

> kalki70 wrote:
> > 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;
> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
handling of regexp objects that aren't referenced by variables,arrays, tables or objects ThomasW Ruby 11 09-28-2009 01:52 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
How to (batch) set EXIF date taken and IPTC creation date and creation time for photos with filenames YYMMDDHHMMSS#.jpg? guercheLE@gmail.com Digital Photography 1 10-04-2005 07:15 PM
Re: Package creation: Why does order matter? SpaceCowboy Java 6 08-14-2003 05:44 PM



Advertisments