vector<Foo> how to force default constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lchian@yahoo.com

    #1

    vector<Foo> how to force default constructor

    Hi,

    I have a vector of class Foo.

    When I do a push_back(), I expect stl to call the default constructor
    I wrote for Foo. But instead, stl makes up its own default that is
    initialized with garbage.

    Is there a way to force stl to use MY default constructor?

    Thanks

  • atgraham@gmail.com

    #2
    Re: vector&lt;Foo&g t; how to force default constructor

    We need to see some code.

    Comment

    • lchian@yahoo.com

      #3
      Re: vector&lt;Foo&g t; how to force default constructor

      Sorry, I typed the whole thing then the network went down.

      class Foo
      {
      public:
      vector<FooBar> vec;
      .......
      }

      class FooBar
      {
      public:
      FooBar(0;
      FooBar(const Foobar &f){*this=f; }
      Foobar &operator=(cons t FooBar &f);
      ~FooBar();
      .......
      }

      int main()
      {
      Foo me;

      FooBar b;
      ......

      me.vec.push_bac k(b);
      }

      Comment

      • atgraham@gmail.com

        #4
        Re: vector&lt;Foo&g t; how to force default constructor

        I don't see anything wrong here. The FooBar() constructor is called
        automatically for "b" before push_back() is called. The FooBar copy
        constructor is unnecessary; that's the default behavior.

        Most likely the bug is somewhere else in your code.

        Comment

        • lchian@yahoo.com

          #5
          Re: vector&lt;Foo&g t; how to force default constructor

          It is related to push_back.

          As I understand it, STL always insert a copy of the object. in this
          case, a copy of "b".

          First, it calls the (wrong) default constructor, then use the
          assignment operator to assign the data inside "b" to the newly created
          object, then put it into "vec".

          Comment

          • atgraham@gmail.com

            #6
            Re: vector&lt;Foo&g t; how to force default constructor

            That's right; a copy of "b" will be inserted into the vector. However,
            I can't see your implementation of the FooBar() constructor. The bug
            could be there. Your implementation of the copy constructor uses the
            assignment operator. But I can't see your implementation of the
            assignment operator either, so the bug could be there as well.
            Nonetheless, The fact that you've attempted to define your own copy and
            assignment constructor is curious. If you want it to exhibit the
            default behavior, then don't define them at all. Otherwise, the bug is
            probably there.

            Comment

            • BobR

              #7
              Re: vector&lt;Foo&g t; how to force default constructor


              lchian@yahoo.co m wrote in message[color=blue]
              >
              >class FooBar{
              > public:
              > FooBar(0;[/color]

              FooBar(); // This should help
              [color=blue]
              > FooBar(const Foobar &f){*this=f; }
              > Foobar &operator=(cons t FooBar &f);
              > ~FooBar();
              > .......
              >}[/color]

              --
              Bob R
              POVrookie


              Comment

              • lchian@yahoo.com

                #8
                Re: vector&lt;Foo&g t; how to force default constructor

                There was a typo: it should be FooBar(), not FooBar(0, fogot the shift
                key.

                Anyway, I put break point in the default constructor and found it was
                never called.

                Comment

                • Michiel.Salters@tomtom.com

                  #9
                  Re: vector&lt;Foo&g t; how to force default constructor


                  lchian@yahoo.co m wrote:[color=blue]
                  > It is related to push_back.
                  >
                  > As I understand it, STL always insert a copy of the object. in this
                  > case, a copy of "b".[/color]

                  True, that is what logically happens.
                  [color=blue]
                  > First, it calls the (wrong) default constructor, then use the
                  > assignment operator to assign the data inside "b" to the newly created
                  > object, then put it into "vec".[/color]

                  That is apparently what your implementation does. However, part of the
                  vector<T> specification is that T must have proper object semantics.
                  Apparently, your FooBar doesn't, and thus vector<FooBar> doesn't work.

                  That said, I'm not 100% sure if push_back officially requires the
                  default ctor.
                  Some vector members do, some don't, and I'd have to look up the exact
                  rules. Personally, I just stick to "int semantics".

                  HTH,
                  Michiel Salters

                  Comment

                  • jollygoodfellow@sina.com.cn

                    #10
                    Re: vector&lt;Foo&g t; how to force default constructor

                    "Apparently , your FooBar doesn't, and thus vector<FooBar> doesn't
                    work."

                    Its a typo, like the man said.

                    Lets get on with the answers.

                    Comment

                    • Andrew Koenig

                      #11
                      Re: vector&lt;Foo&g t; how to force default constructor

                      <lchian@yahoo.c om> wrote in message
                      news:1136160755 .473588.107460@ g14g2000cwa.goo glegroups.com.. .
                      [color=blue]
                      > class FooBar
                      > {
                      > public:
                      > FooBar(0;[/color]

                      This line shouldn't compile. I'm assuming you meant FooBar();
                      [color=blue]
                      > FooBar(const Foobar &f){*this=f; }
                      > Foobar &operator=(cons t FooBar &f);
                      > ~FooBar();
                      > .......
                      > }
                      >
                      > int main()
                      > {
                      > Foo me;
                      >
                      > FooBar b;
                      > ......
                      >
                      > me.vec.push_bac k(b);
                      > }
                      >[/color]

                      This should do the following:

                      Call the FooBar default constructor to initialize b

                      Construct a new last element for me.vec by copying b into it.

                      So if you think that something else is happening, we need to see what the
                      default constructor for FooBar is doing, and also what your evidence is that
                      your program is not doing the right thing.


                      Comment

                      • Andrew Koenig

                        #12
                        Re: vector&lt;Foo&g t; how to force default constructor

                        <lchian@yahoo.c om> wrote in message
                        news:1136174815 .714608.78260@g 49g2000cwa.goog legroups.com...
                        [color=blue]
                        > Anyway, I put break point in the default constructor and found it was
                        > never called.[/color]

                        Please post a complete program that shows the problem without the need to
                        set breakpoints.


                        Comment

                        • Ron Natalie

                          #13
                          Re: vector&lt;Foo&g t; how to force default constructor

                          lchian@yahoo.co m wrote:[color=blue]
                          > Sorry, I typed the whole thing then the network went down.
                          >
                          > class Foo
                          > {
                          > public:
                          > vector<FooBar> vec;
                          > .......
                          > }
                          >
                          > class FooBar
                          > {
                          > public:
                          > FooBar(0;
                          > FooBar(const Foobar &f){*this=f; }
                          >[/color]
                          This copy constructor is highly suspect. You do know that
                          constructors are mutually exclusive? Only one ever runs
                          for an object. You assign into an uninitaizized *this here.


                          Comment

                          • Ron Natalie

                            #14
                            Re: vector&lt;Foo&g t; how to force default constructor

                            lchian@yahoo.co m wrote:
                            [color=blue]
                            > First, it calls the (wrong) default constructor, then use the
                            > assignment operator to assign the data inside "b" to the newly created
                            > object, then put it into "vec".
                            >[/color]
                            That's how vectors work. Your objects are free to be copied either
                            by the assignment operator or the copy constructor at various times
                            in the vector. The object MUST work in light of that.

                            A default constructor is not necessary. The few places where a
                            non-copy-initialized object is used the call has an additional
                            argument which is the "prototype" object to copy (it's just defaulted
                            to a default constructed object).

                            Your use of the term "wrong" with "default constructor" is confusing.
                            There is only one default constructor. If you have more than one,
                            your program is ill-formed.

                            Comment

                            • Mirek Fidler

                              #15
                              Re: vector&lt;Foo&g t; how to force default constructor

                              lchian@yahoo.co m wrote:[color=blue]
                              > Hi,
                              >
                              > I have a vector of class Foo.
                              >
                              > When I do a push_back(), I expect stl to call the default constructor
                              > I wrote for Foo. But instead, stl makes up its own default that is
                              > initialized with garbage.[/color]

                              Actually, std::vector never calls the default constructor for T during
                              push_back (nor any other operation AFAIK), just the copy constructor.

                              Mirek

                              Comment

                              Working...