Order of creation of objects in arrays

Discussion in 'C++' started by kalki70, Sep 4, 2006.

  1. kalki70

    kalki70 Guest

    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
     
    kalki70, Sep 4, 2006
    #1
    1. Advertising

  2. kalki70

    Guest

    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
     
    , Sep 4, 2006
    #2
    1. Advertising

  3. kalki70

    Guest

    #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;
    }
    写é“:

    > 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
     
    , Sep 4, 2006
    #3
  4. kalki70

    Todd Gardner Guest

    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;
    }
     
    Todd Gardner, Sep 4, 2006
    #4
  5. kalki70

    kalki70 Guest

    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
     
    kalki70, Sep 13, 2006
    #5
  6. kalki70

    kalki70 Guest

    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;
    > }
     
    kalki70, Sep 13, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SpaceCowboy
    Replies:
    6
    Views:
    531
    SpaceCowboy
    Aug 14, 2003
  2. 7stud
    Replies:
    11
    Views:
    710
    Dennis Lee Bieber
    Mar 20, 2007
  3. Marcob
    Replies:
    3
    Views:
    313
    Michele Simionato
    Jun 23, 2008
  4. Philipp
    Replies:
    21
    Views:
    1,156
    Philipp
    Jan 20, 2009
  5. ThomasW
    Replies:
    11
    Views:
    293
    ThomasW
    Sep 28, 2009
Loading...

Share This Page