Thread: Array initializing

  1. #1
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68

    Array initializing

    hi all,

    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
            public:
            int i;
            A(int i):i(i)
            {
    
                    cout<<"A created "<<i<<endl;
            }
            ~A()
            {
                    cout<<" A Destroyed "<<i<<endl;
            }
            A()
            {
                    cout<<"A() called"<<endl;
            }
    };
    
    class B:public A
    {
            public:
                    B(int j):A(j)
                    {
                            cout<<"B created "<<i<<endl;
                    }
                    B(){ cout <<" B() called"<<endl;}
                    ~B()
                    {
                            cout<<" B distroyed "<<i<<endl;
                    }
              };
    
    int main()
    {
            A a(100);
            B b(200);
            A j[] = {A(1),B(2)};
            return 0;
    }
    the output of this would be

    A created 100
    A created 200
    B created 200
    A created 1
    A created 2
    B created 2
    B distroyed 2
    A Destroyed 2
    A Destroyed 2
    A Destroyed 1
    B distroyed 200
    A Destroyed 200
    A Destroyed 100

    can anyone tell me , how the distructor of B is called, after the initializationg ( A(int) is called, and then when creating B, A(int) is first called, and then B(int), which I understood, what I can't understand is why ~B() is called right after this)

    Thank you in advance
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Destructors are called first on the last things created. Look at the order of your destructors. It's the exact opposite of your constructor order.

    The program's memory is a stack. First In, Last Out.
    Last edited by SlyMaelstrom; 03-23-2006 at 02:43 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Code:
    > A j[] = {A(1),B(2)};
    This is the same as
    Code:
    A j[] = {A(1),A(B(2))};
    Note that an object of B is created to initialize an object of A and then promptly thrown away. If you printed something from A's copy constructor you would get the following for the A(B(2)) -part:

    ...
    A created 2
    B created 2
    A copy created 2
    B distroyed 2
    A Destroyed 2
    A Destroyed 2
    ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Initializing a 2D Array in C
    By Cell in forum C Programming
    Replies: 20
    Last Post: 03-21-2009, 12:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 05:19 PM