constructor initialization list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Makis Papapanagiotou

    constructor initialization list

    Hello all,

    There is a strange case where a class Test is composed from three objects of
    other classes. i.e.

    class Test
    { public:
    Test();
    private:
    Point x;
    Cat y;
    Dog z;
    };

    where the classes Point, Cat, Dog are

    class Point
    { public:
    Point();
    Point(Point& x, Dog& y);
    private:
    };

    class Cat
    { public:
    Cat();
    Cat(Point& x, Dog& y);
    private:
    };

    class Dog
    { public:
    Dog();
    Dog(Point& x, Cat& y);
    private:
    };

    If we were calling the default constructors they is no problem, since as
    STROUSTRUP is wrtiing in his book they are called in the order they are
    defined in the class the main class (Test).

    My big problem is why the following is working? i.e when the other
    constructors are called.

    Test::Test():x( ), y(x,z), z(x,y)

    since the creation of object "y" is related with the creation of object "z".

    Can any body give an explanation of this strange behaviour?

    Thanks in advance,


  • JKop

    #2
    Re: constructor initialization list

    [color=blue]
    > Can any body give an explanation of this strange behaviour?[/color]


    The objects are contructed in the order in which they are, in the
    Initialization List.

    For objects that *aren't* listed in the initialization list, they are
    constructed *before* all other member objects. So, given the following:


    #include <string>

    class Blah
    {
    public:
    std::string a;
    std::string b;
    std::string c;
    std::string d;
    std::string e;
    std::string f;

    Blah() : c("Monkey!"), e(), a(), f("Ape!");
    };


    The order of in which the member objects are consructed in the above is as
    follows:

    b
    d
    c
    e
    a
    f


    -JKop


    Comment

    • JKop

      #3
      Re: constructor initialization list

      [color=blue]
      > Test::Test():x( ), y(x,z), z(x,y)[/color]


      Opps! I missed the point with my last post.


      The above code is ill-formed.


      -JKop

      Comment

      • Karl Heinz Buchegger

        #4
        Re: constructor initialization list

        Makis Papapanagiotou wrote:[color=blue]
        >
        > Hello all,
        >
        > There is a strange case where a class Test is composed from three objects of
        > other classes. i.e.
        >
        > class Test
        > { public:
        > Test();
        > private:
        > Point x;
        > Cat y;
        > Dog z;
        > };
        >
        > where the classes Point, Cat, Dog are
        >
        > class Point
        > { public:
        > Point();
        > Point(Point& x, Dog& y);
        > private:
        > };
        >
        > class Cat
        > { public:
        > Cat();
        > Cat(Point& x, Dog& y);
        > private:
        > };
        >
        > class Dog
        > { public:
        > Dog();
        > Dog(Point& x, Cat& y);
        > private:
        > };
        >
        > If we were calling the default constructors they is no problem, since as
        > STROUSTRUP is wrtiing in his book they are called in the order they are
        > defined in the class the main class (Test).
        >
        > My big problem is why the following is working?[/color]

        Define 'working'
        [color=blue]
        > i.e when the other
        > constructors are called.[/color]

        It is still the same order:
        x gets constructed first
        y gets constructed next
        z gets constructed last
        [color=blue]
        >
        > Test::Test():x( ), y(x,z), z(x,y)[/color]

        The order in which constructors of member objects are called
        is entirely defined by the order in which they are listed
        in the class declaration. Here, in the initializer list
        you simply specify *which* constructor to use for the members,
        not the order in which they are called.

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • Makis Papapanagiotou

          #5
          Re: constructor initialization list

          Hello,

          Sorry but this is not covering my question since in your example the objects
          are not related to each other.

          In my case they are

          Test:: x( ), y(x,z), z(x,y)

          So the object "y" in order to be constructed needs the object "z", but "z"
          in order to be constructed needs "y" first.
          Even though they are constructed.

          Hence how this is done? Because it is done and it is not an error !!!


          Thanks,

          "JKop" <NULL@NULL.NULL > wrote in message
          news:Xn5id.4076 2$Z14.15542@new s.indigo.ie...[color=blue]
          >[color=green]
          > > Can any body give an explanation of this strange behaviour?[/color]
          >
          >
          > The objects are contructed in the order in which they are, in the
          > Initialization List.
          >
          > For objects that *aren't* listed in the initialization list, they are
          > constructed *before* all other member objects. So, given the following:
          >
          >
          > #include <string>
          >
          > class Blah
          > {
          > public:
          > std::string a;
          > std::string b;
          > std::string c;
          > std::string d;
          > std::string e;
          > std::string f;
          >
          > Blah() : c("Monkey!"), e(), a(), f("Ape!");
          > };
          >
          >
          > The order of in which the member objects are consructed in the above is as
          > follows:
          >
          > b
          > d
          > c
          > e
          > a
          > f
          >
          >
          > -JKop
          >
          >[/color]


          Comment

          • Karl Heinz Buchegger

            #6
            Re: constructor initialization list

            JKop wrote:[color=blue]
            >[color=green]
            > > Can any body give an explanation of this strange behaviour?[/color]
            >
            > The objects are contructed in the order in which they are, in the
            > Initialization List.[/color]

            You might want to reread on this.
            The order is defined entirely by the order the members are listed
            in the class declaration. The initialzation list just selects which
            constructor to use, but does not change the order in which members
            get initialized.


            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            • Makis Papapanagiotou

              #7
              Re: constructor initialization list

              Hello,

              Yes it is still in the same order:

              x gets constructed first
              y gets constructed next
              z gets constructed last

              At least thats what we see if we put a "cout" stetement in the
              constructors...

              Thanks,

              "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
              news:4188E37A.F 9A4A6E3@gascad. at...[color=blue]
              > Makis Papapanagiotou wrote:[color=green]
              > >
              > > Hello all,
              > >
              > > There is a strange case where a class Test is composed from three[/color][/color]
              objects of[color=blue][color=green]
              > > other classes. i.e.
              > >
              > > class Test
              > > { public:
              > > Test();
              > > private:
              > > Point x;
              > > Cat y;
              > > Dog z;
              > > };
              > >
              > > where the classes Point, Cat, Dog are
              > >
              > > class Point
              > > { public:
              > > Point();
              > > Point(Point& x, Dog& y);
              > > private:
              > > };
              > >
              > > class Cat
              > > { public:
              > > Cat();
              > > Cat(Point& x, Dog& y);
              > > private:
              > > };
              > >
              > > class Dog
              > > { public:
              > > Dog();
              > > Dog(Point& x, Cat& y);
              > > private:
              > > };
              > >
              > > If we were calling the default constructors they is no problem, since as
              > > STROUSTRUP is wrtiing in his book they are called in the order they are
              > > defined in the class the main class (Test).
              > >
              > > My big problem is why the following is working?[/color]
              >
              > Define 'working'
              >[color=green]
              > > i.e when the other
              > > constructors are called.[/color]
              >
              > It is still the same order:
              > x gets constructed first
              > y gets constructed next
              > z gets constructed last
              >[color=green]
              > >
              > > Test::Test():x( ), y(x,z), z(x,y)[/color]
              >
              > The order in which constructors of member objects are called
              > is entirely defined by the order in which they are listed
              > in the class declaration. Here, in the initializer list
              > you simply specify *which* constructor to use for the members,
              > not the order in which they are called.
              >
              > --
              > Karl Heinz Buchegger
              > kbuchegg@gascad .at[/color]


              Comment

              • JKop

                #8
                Re: constructor initialization list

                Karl Heinz Buchegger posted:
                [color=blue]
                > JKop wrote:[color=green]
                >>[color=darkred]
                >> > Can any body give an explanation of this strange behaviour?[/color]
                >>
                >> The objects are contructed in the order in which they are, in the
                >> Initialization List.[/color]
                >
                > You might want to reread on this.
                > The order is defined entirely by the order the members are listed
                > in the class declaration. The initialzation list just selects which
                > constructor to use, but does not change the order in which members
                > get initialized.[/color]


                I'm wrong!


                I wasn't exactly sure so I went off and tested it, but then I realized
                that my "test" would have produced the same results either way.

                So... in summation:

                Objects are constructed in the order in which they appear in the class
                declaration.

                (I think what had me confused is where you have multiple base classes
                and you use the initialization list to specify the order in which the
                base classes' constructors are called.)


                -JKop

                Comment

                • Karl Heinz Buchegger

                  #9
                  Re: constructor initialization list

                  Makis Papapanagiotou wrote:[color=blue]
                  >
                  > Hello,
                  >
                  > Sorry but this is not covering my question since in your example the objects
                  > are not related to each other.
                  >
                  > In my case they are
                  >
                  > Test:: x( ), y(x,z), z(x,y)
                  >
                  > So the object "y" in order to be constructed needs the object "z", but "z"
                  > in order to be constructed needs "y" first.
                  > Even though they are constructed.
                  >
                  > Hence how this is done? Because it is done and it is not an error !![/color]

                  It all depends on what the Cat or Dog constructor do with
                  the passed reference.

                  When the Cat constructor is called and passed the Dog object,
                  the Dog object isn't yet fully constructed, but the memory for
                  that object has already been reserved. So for the Cat object it
                  is safe to eg. store a pointer to the Dog object or create a reference
                  to it. You, the programmer, know that at exactly this memory location
                  eventually there will be a fully constructed Dog object, when the
                  whole initialization is completed.


                  --
                  Karl Heinz Buchegger
                  kbuchegg@gascad .at

                  Comment

                  • JKop

                    #10
                    Re: constructor initialization list

                    JKop posted:
                    [color=blue]
                    >[color=green]
                    >> Test::Test():x( ), y(x,z), z(x,y)[/color]
                    >
                    >
                    > Opps! I missed the point with my last post.
                    >
                    >
                    > The above code is ill-formed.
                    >
                    >
                    > -JKop[/color]


                    Again I'm wrong!

                    Comment

                    • Karl Heinz Buchegger

                      #11
                      Re: constructor initialization list

                      Makis Papapanagiotou wrote:[color=blue]
                      >
                      > Hello,
                      >
                      > Yes it is still in the same order:
                      >
                      > x gets constructed first
                      > y gets constructed next
                      > z gets constructed last
                      >
                      > At least thats what we see if we put a "cout" stetement in the
                      > constructors...[/color]

                      Well. I got your question wrong, mostly because you didn't ask
                      a specific question other then: "Why is this <lots of code> not wrong?"
                      Moral of story: If you want to emphasize on something, code is a good
                      thing, but tell us exactly what is puzzeling you.

                      See my other reply for some explanations.

                      --
                      Karl Heinz Buchegger
                      kbuchegg@gascad .at

                      Comment

                      • Jeff Flinn

                        #12
                        Re: constructor initialization list


                        "JKop" <NULL@NULL.NULL > wrote in message
                        news:CB5id.4076 4$Z14.15291@new s.indigo.ie...[color=blue]
                        > Karl Heinz Buchegger posted:
                        >[color=green]
                        > > JKop wrote:[color=darkred]
                        > >>
                        > >> > Can any body give an explanation of this strange behaviour?
                        > >>
                        > >> The objects are contructed in the order in which they are, in the
                        > >> Initialization List.[/color]
                        > >
                        > > You might want to reread on this.
                        > > The order is defined entirely by the order the members are listed
                        > > in the class declaration. The initialzation list just selects which
                        > > constructor to use, but does not change the order in which members
                        > > get initialized.[/color]
                        >
                        >
                        > I'm wrong!
                        >
                        >
                        > I wasn't exactly sure so I went off and tested it, but then I realized
                        > that my "test" would have produced the same results either way.
                        >
                        > So... in summation:
                        >
                        > Objects are constructed in the order in which they appear in the class
                        > declaration.
                        >
                        > (I think what had me confused is where you have multiple base classes
                        > and you use the initialization list to specify the order in which the
                        > base classes' constructors are called.)[/color]

                        Wrong again.

                        Jeff F


                        Comment

                        • Gianni Mariani

                          #13
                          Re: constructor initialization list

                          JKop wrote:[color=blue][color=green]
                          >>Can any body give an explanation of this strange behaviour?[/color]
                          >
                          >
                          >
                          > The objects are contructed in the order in which they are, in the
                          > Initialization List.[/color]


                          I don't think that's right. The order of member construction is the
                          order in which they are defined in the class. The order of member
                          destruction is the reverse order in which they are defined in the class,
                          regardless of the order of the initializer list. Inherited classes are
                          constructed before members in the order in which they are inherited and
                          destructed in reverse order of construction.

                          #include <iostream>

                          template <int num>
                          class Yell
                          {
                          public:
                          int m_num;

                          Yell()
                          : m_num()
                          {
                          std::cout << num << " Default" << std::endl;
                          }

                          Yell( int i )
                          : m_num( i )
                          {
                          std::cout << num << " Nondefault( " << i << " )" << std::endl;
                          }

                          ~Yell()
                          {
                          std::cout << num << " DESTRUCT( " << m_num << " )" << std::endl;
                          }

                          };

                          class Blah
                          : public Yell<10>, public Yell<11>
                          {
                          public:
                          Yell<1> a;
                          Yell<2> b;
                          Yell<3> c;
                          Yell<4> d;
                          Yell<5> e;
                          Yell<6> f;

                          Blah() : c(100), e(), a(), f(200), Yell<11>( 300 )
                          {
                          }
                          };

                          int main()
                          {
                          Blah x;
                          }

                          output:

                          10 Default
                          11 Nondefault( 300 )
                          1 Default
                          2 Default
                          3 Nondefault( 100 )
                          4 Default
                          5 Default
                          6 Nondefault( 200 )
                          6 DESTRUCT( 200 )
                          5 DESTRUCT( 0 )
                          4 DESTRUCT( 0 )
                          3 DESTRUCT( 100 )
                          2 DESTRUCT( 0 )
                          1 DESTRUCT( 0 )
                          11 DESTRUCT( 300 )
                          10 DESTRUCT( 0 )

                          Comment

                          • Ron Natalie

                            #14
                            Re: constructor initialization list

                            JKop wrote:[color=blue][color=green]
                            >>Can any body give an explanation of this strange behaviour?[/color]
                            >
                            >
                            >
                            > The objects are contructed in the order in which they are, in the
                            > Initialization List.
                            >
                            > For objects that *aren't* listed in the initialization list, they are
                            > constructed *before* all other member objects. So, given the following:[/color]

                            It doesn't matter whether they are listed or NOT.
                            The initilization list has NO BEARING on the order of connstruction.


                            Comment

                            • Chris Theis

                              #15
                              Re: constructor initialization list


                              "JKop" <NULL@NULL.NULL > wrote in message
                              news:CB5id.4076 4$Z14.15291@new s.indigo.ie...[color=blue]
                              > Karl Heinz Buchegger posted:
                              >[color=green]
                              > > JKop wrote:[color=darkred]
                              > >>
                              > >> > Can any body give an explanation of this strange behaviour?
                              > >>
                              > >> The objects are contructed in the order in which they are, in the
                              > >> Initialization List.[/color]
                              > >
                              > > You might want to reread on this.
                              > > The order is defined entirely by the order the members are listed
                              > > in the class declaration. The initialzation list just selects which
                              > > constructor to use, but does not change the order in which members
                              > > get initialized.[/color]
                              >
                              >
                              > I'm wrong!
                              >
                              >
                              > I wasn't exactly sure so I went off and tested it, but then I realized
                              > that my "test" would have produced the same results either way.
                              >
                              > So... in summation:
                              >
                              > Objects are constructed in the order in which they appear in the class
                              > declaration.
                              >
                              > (I think what had me confused is where you have multiple base classes
                              > and you use the initialization list to specify the order in which the
                              > base classes' constructors are called.)
                              >[/color]

                              Sorry to tell you, but you´re wrong again. The initialization list does not
                              specify the order of execution of the base class ctors. To make a long
                              matter short - read the FAQ, item 25.14.

                              Chris


                              Comment

                              Working...