Simple constructor question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    Simple constructor question

    (if this is a FAQ, I apologize - I didn't see it)

    I have the following class declaration:

    class TMSViewDayList : public TMSViewDayListB ase {
    public:
    uint start;
    uint end;
    TMSViewDayList( ) : start(0),end(0) {}
    };

    TMSViewDayListB ase is a template class that acts something like a vector. My
    question is, is TMSViewDayListB ase's default constructor implicitly called
    here, or do I need to explicitly call it like

    TMSViewDayList( ) : start(0),end(0) ,TMSViewDayList Base() {}

    ?

    --
    Christopher Benson-Manica | Upon the wheel thy fate doth turn,
    ataru(at)cybers pace.org | upon the rack thy lesson learn.
  • Gianni Mariani

    #2
    Re: Simple constructor question

    Christopher Benson-Manica wrote:[color=blue]
    > (if this is a FAQ, I apologize - I didn't see it)
    >
    > I have the following class declaration:
    >
    > class TMSViewDayList : public TMSViewDayListB ase {
    > public:
    > uint start;
    > uint end;
    > TMSViewDayList( ) : start(0),end(0) {}
    > };
    >
    > TMSViewDayListB ase is a template class that acts something like a vector. My
    > question is, is TMSViewDayListB ase's default constructor implicitly called
    > here, or do I need to explicitly call it like
    >
    > TMSViewDayList( ) : start(0),end(0) ,TMSViewDayList Base() {}[/color]

    It's implicit.

    Easy to test with a few lines of example code.

    The order of evaluation is interesting as well.

    Comment

    • jeffc

      #3
      Re: Simple constructor question


      "Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
      news:bm12sq$asq $1@chessie.cirr .com...[color=blue]
      > (if this is a FAQ, I apologize - I didn't see it)
      >
      > I have the following class declaration:
      >
      > class TMSViewDayList : public TMSViewDayListB ase {
      > public:
      > uint start;
      > uint end;
      > TMSViewDayList( ) : start(0),end(0) {}
      > };
      >
      > TMSViewDayListB ase is a template class that acts something like a vector.[/color]
      My[color=blue]
      > question is, is TMSViewDayListB ase's default constructor implicitly called[/color]

      Yes.


      Comment

      • David Rubin

        #4
        Re: Simple constructor question

        Gianni Mariani wrote:

        [snip][color=blue]
        > The order of evaluation is interesting as well.[/color]

        This is interesting. For example:

        class B
        {
        protected:
        int i;
        public:
        B() : i(10) {}
        };

        class A : public B
        {
        int j;
        public:
        A() : i(20), j(30) {}
        };

        int
        main()
        {
        A a;
        return 0;
        }

        ; g++ derive.cc
        derive.cc: In constructor `A::A()':
        derive.cc:14: error: class `A' does not have any field named `i'

        What is going on here? Assigning i inside the body of A() works, but not
        in the initializer list. Invoking B() in the initialization list before
        i(20) does not work either.

        /david

        --
        Andre, a simple peasant, had only one thing on his mind as he crept
        along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
        -- unknown

        Comment

        • jeffc

          #5
          Re: Simple constructor question


          "David Rubin" <bogus_address@ nomail.com> wrote in message
          news:3F845A96.C 44F3250@nomail. com...[color=blue]
          > Gianni Mariani wrote:
          >
          > [snip][color=green]
          > > The order of evaluation is interesting as well.[/color]
          >
          > This is interesting. For example:
          >
          > class B
          > {
          > protected:
          > int i;
          > public:
          > B() : i(10) {}
          > };
          >
          > class A : public B
          > {
          > int j;
          > public:
          > A() : i(20), j(30) {}
          > };
          >
          > int
          > main()
          > {
          > A a;
          > return 0;
          > }
          >
          > ; g++ derive.cc
          > derive.cc: In constructor `A::A()':
          > derive.cc:14: error: class `A' does not have any field named `i'
          >
          > What is going on here? Assigning i inside the body of A() works, but not
          > in the initializer list. Invoking B() in the initialization list before
          > i(20) does not work either.[/color]

          i doesn't belong in the initializer list for A because it's not a member of
          A. You can assign to i inside the body of A, yes, but you said "not in the
          initializer list". There's a difference between initializing and assigning
          that's important in this case. It's called the "initialize r list" instead
          of the "assignment list" because it's initialization, not assignment. If
          you assign to i in the body of A, it's already been initialized by B.


          Comment

          • David Rubin

            #6
            Re: Simple constructor question

            jeffc wrote:

            [snip][color=blue][color=green]
            > > What is going on here? Assigning i inside the body of A() works, but not
            > > in the initializer list. Invoking B() in the initialization list before
            > > i(20) does not work either.[/color]
            >
            > i doesn't belong in the initializer list for A because it's not a member of
            > A. You can assign to i inside the body of A, yes, but you said "not in the
            > initializer list". There's a difference between initializing and assigning
            > that's important in this case. It's called the "initialize r list" instead
            > of the "assignment list" because it's initialization, not assignment. If
            > you assign to i in the body of A, it's already been initialized by B.[/color]

            The confusion is that A inherits member i from B. Therefore, why is i
            not a member of A when A is being constructed? It would follow that if i
            is a member of A, i can be initialized in A(), but this is clearly not
            the case.

            /david

            --
            Andre, a simple peasant, had only one thing on his mind as he crept
            along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
            -- unknown

            Comment

            • Ron Natalie

              #7
              Re: Simple constructor question


              "David Rubin" <bogus_address@ nomail.com> wrote in message news:3F84740E.1 024DEAF@nomail. com...
              [color=blue]
              > The confusion is that A inherits member i from B. Therefore, why is i
              > not a member of A when A is being constructed? It would follow that if i
              > is a member of A, i can be initialized in A(), but this is clearly not
              > the case.[/color]

              Inheritance is not the issue. The initializer list is restricted to DIRECT bases,
              VIRUTAL bases, and members of the class being initialized. Members of
              base classes (or non-direct base classes) are not eligible.

              There's some ambiguity here. Which value should B::i be initialized with 10 or 20?


              Comment

              • Julián Albo

                #8
                Re: Simple constructor question

                David Rubin escribió:
                [color=blue]
                > The confusion is that A inherits member i from B. Therefore, why is i
                > not a member of A when A is being constructed? It would follow that if i
                > is a member of A, i can be initialized in A(), but this is clearly not
                > the case.[/color]

                It is a member of B, then it gets initialized during the initialization
                of B. The if initilize it in the constructor of A, it will be
                initialized two times, and that is not desired in C++.

                Regards.

                Comment

                • jeffc

                  #9
                  Re: Simple constructor question


                  "David Rubin" <bogus_address@ nomail.com> wrote in message
                  news:3F84740E.1 024DEAF@nomail. com...[color=blue][color=green]
                  > >
                  > > i doesn't belong in the initializer list for A because it's not a member[/color][/color]
                  of[color=blue][color=green]
                  > > A. You can assign to i inside the body of A, yes, but you said "not in[/color][/color]
                  the[color=blue][color=green]
                  > > initializer list". There's a difference between initializing and[/color][/color]
                  assigning[color=blue][color=green]
                  > > that's important in this case. It's called the "initialize r list"[/color][/color]
                  instead[color=blue][color=green]
                  > > of the "assignment list" because it's initialization, not assignment.[/color][/color]
                  If[color=blue][color=green]
                  > > you assign to i in the body of A, it's already been initialized by B.[/color]
                  >
                  > The confusion is that A inherits member i from B. Therefore, why is i
                  > not a member of A when A is being constructed? It would follow that if i
                  > is a member of A, i can be initialized in A(), but this is clearly not
                  > the case.[/color]

                  But i is *never* a member of A, not when A is being constructed, and not
                  after it's been constructed. A does have access rights to i, but i is a
                  member of B, not of A. I don't think it's accurate to say that A inherits
                  member i from B (someone correct me if I'm wrong). But even if it were OK
                  to use the terminology that A inherits i from B, you still would not say
                  that i is a member of A.


                  Comment

                  • jeffc

                    #10
                    Re: Simple constructor question


                    "Ron Natalie" <ron@sensor.com > wrote in message
                    news:3f8478ae$0 $36946$9a6e19ea @news.newshosti ng.com...[color=blue]
                    >
                    > "David Rubin" <bogus_address@ nomail.com> wrote in message[/color]
                    news:3F84740E.1 024DEAF@nomail. com...[color=blue]
                    >[color=green]
                    > > The confusion is that A inherits member i from B. Therefore, why is i
                    > > not a member of A when A is being constructed? It would follow that if i
                    > > is a member of A, i can be initialized in A(), but this is clearly not
                    > > the case.[/color]
                    >
                    > Inheritance is not the issue. The initializer list is restricted to[/color]
                    DIRECT bases,[color=blue]
                    > VIRUTAL bases, and members of the class being initialized.[/color]

                    But inheritance is the issue from his point of view, because he believes i
                    IS a member of the class being initialized (precisely because A inherits
                    from B.)


                    Comment

                    • Ron Natalie

                      #11
                      Re: Simple constructor question


                      "Julián Albo" <JULIANALBO@ter ra.es> wrote in message news:3F847A2E.3 2CBC0A8@terra.e s...
                      [color=blue]
                      > It is a member of B, then it gets initialized during the initialization
                      > of B. The if initilize it in the constructor of A, it will be
                      > initialized two times, and that is not desired in C++.[/color]

                      Not only not desired, it's not even possible.


                      Comment

                      • Julián Albo

                        #12
                        Re: Simple constructor question

                        Ron Natalie escribió:
                        [color=blue][color=green]
                        > > It is a member of B, then it gets initialized during the initialization
                        > > of B. The if initilize it in the constructor of A, it will be
                        > > initialized two times, and that is not desired in C++.[/color]
                        > Not only not desired, it's not even possible.[/color]

                        Yeah, and it's not possible because the designers of the language found
                        did not desire it. I was trying to explain why is not allowed.

                        Regards.

                        Comment

                        Working...