STL-container used with references

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Severin Ecker

    STL-container used with references

    hi!

    normally i would simply do the following:

    std::vector<Ele ment> vec;
    void somefunc() {
    Element e;
    vec.push_back(e );
    }

    now Element e is in the vector. thats fine as long as no polymorphic
    behaviour is needed.

    std::vector<Ele ment &> vec;
    void somefunc() {
    Derived_from_El ement e;
    vec.push_back(e ); //bad idea,.. e is gone after functionscope is left
    }

    what i want to avoid (if possible) are 2 things:
    dynamic allocation of the objects, and having an extracontiner for every
    type derived from the basetype to store the elements.

    i hope, i made clear what i'm trying to do... so is there a solution (except
    for the 2 mentioned above?)

    thx, regards,
    sev


  • tom_usenet

    #2
    Re: STL-container used with references

    On Tue, 6 Apr 2004 15:04:53 +0200, "Severin Ecker" <secker@gmx.a t>
    wrote:
    [color=blue]
    >hi!
    >
    >normally i would simply do the following:
    >
    >std::vector<El ement> vec;
    >void somefunc() {
    > Element e;
    > vec.push_back(e );
    >}
    >
    >now Element e is in the vector. thats fine as long as no polymorphic
    >behaviour is needed.
    >
    >std::vector<El ement &> vec;[/color]

    You can't hold references in containers - references are just aliases,
    not real objects.
    [color=blue]
    >void somefunc() {
    > Derived_from_El ement e;
    > vec.push_back(e ); //bad idea,.. e is gone after functionscope is left
    >}
    >
    >what i want to avoid (if possible) are 2 things:
    >dynamic allocation of the objects, and having an extracontiner for every
    >type derived from the basetype to store the elements.
    >
    >i hope, i made clear what i'm trying to do... so is there a solution (except
    >for the 2 mentioned above?)[/color]

    It is obviously hard to avoid dynamic allocation of the objects, since
    all of your derived types can have different sizes and alignment
    requirements. There are techniques for doing it (as long as all
    derived classes are known in advance), but they would be premature
    optimization in this case I am sure. Your best bet is to use a
    container of smart pointers:

    std::vector<sha red_ptr<Element > > vec;
    vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));

    See www.boost.org for shared_ptr.

    Tom
    --
    C++ FAQ: http://www.parashift.com/c++-faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

    Comment

    • tom_usenet

      #3
      Re: STL-container used with references

      On Tue, 6 Apr 2004 15:04:53 +0200, "Severin Ecker" <secker@gmx.a t>
      wrote:
      [color=blue]
      >hi!
      >
      >normally i would simply do the following:
      >
      >std::vector<El ement> vec;
      >void somefunc() {
      > Element e;
      > vec.push_back(e );
      >}
      >
      >now Element e is in the vector. thats fine as long as no polymorphic
      >behaviour is needed.
      >
      >std::vector<El ement &> vec;[/color]

      You can't hold references in containers - references are just aliases,
      not real objects.
      [color=blue]
      >void somefunc() {
      > Derived_from_El ement e;
      > vec.push_back(e ); //bad idea,.. e is gone after functionscope is left
      >}
      >
      >what i want to avoid (if possible) are 2 things:
      >dynamic allocation of the objects, and having an extracontiner for every
      >type derived from the basetype to store the elements.
      >
      >i hope, i made clear what i'm trying to do... so is there a solution (except
      >for the 2 mentioned above?)[/color]

      It is obviously hard to avoid dynamic allocation of the objects, since
      all of your derived types can have different sizes and alignment
      requirements. There are techniques for doing it (as long as all
      derived classes are known in advance), but they would be premature
      optimization in this case I am sure. Your best bet is to use a
      container of smart pointers:

      std::vector<sha red_ptr<Element > > vec;
      vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));

      See www.boost.org for shared_ptr.

      Tom
      --
      C++ FAQ: http://www.parashift.com/c++-faq-lite/
      C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

      Comment

      • Howard

        #4
        Re: STL-container used with references


        "tom_usenet " <tom_usenet@hot mail.com> wrote in message[color=blue]
        > ...Your best bet is to use a
        > container of smart pointers:
        >
        > std::vector<sha red_ptr<Element > > vec;
        > vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));
        >
        > See www.boost.org for shared_ptr.
        >[/color]

        Just curious...What if you don't have (or want) boost? Is there an STL
        solution similar to shared_ptr?

        -Howard


        Comment

        • Howard

          #5
          Re: STL-container used with references


          "tom_usenet " <tom_usenet@hot mail.com> wrote in message[color=blue]
          > ...Your best bet is to use a
          > container of smart pointers:
          >
          > std::vector<sha red_ptr<Element > > vec;
          > vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));
          >
          > See www.boost.org for shared_ptr.
          >[/color]

          Just curious...What if you don't have (or want) boost? Is there an STL
          solution similar to shared_ptr?

          -Howard


          Comment

          • John Harrison

            #6
            Re: STL-container used with references


            "Howard" <alicebt@hotmai l.com> wrote in message
            news:c4uhgn$flm @dispatch.conce ntric.net...[color=blue]
            >
            > "tom_usenet " <tom_usenet@hot mail.com> wrote in message[color=green]
            > > ...Your best bet is to use a
            > > container of smart pointers:
            > >
            > > std::vector<sha red_ptr<Element > > vec;
            > > vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));
            > >
            > > See www.boost.org for shared_ptr.
            > >[/color]
            >
            > Just curious...What if you don't have (or want) boost? Is there an STL
            > solution similar to shared_ptr?
            >[/color]

            No, but it's really very simple to roll your own basic smart pointer. It
            would not have all the functionality of boost's but would certainly support
            polymorphism and automatic cleanup.

            Have a look at Scott Meyers book for example code (I think its the More
            Effective C++ one).

            john


            Comment

            • John Harrison

              #7
              Re: STL-container used with references


              "Howard" <alicebt@hotmai l.com> wrote in message
              news:c4uhgn$flm @dispatch.conce ntric.net...[color=blue]
              >
              > "tom_usenet " <tom_usenet@hot mail.com> wrote in message[color=green]
              > > ...Your best bet is to use a
              > > container of smart pointers:
              > >
              > > std::vector<sha red_ptr<Element > > vec;
              > > vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));
              > >
              > > See www.boost.org for shared_ptr.
              > >[/color]
              >
              > Just curious...What if you don't have (or want) boost? Is there an STL
              > solution similar to shared_ptr?
              >[/color]

              No, but it's really very simple to roll your own basic smart pointer. It
              would not have all the functionality of boost's but would certainly support
              polymorphism and automatic cleanup.

              Have a look at Scott Meyers book for example code (I think its the More
              Effective C++ one).

              john


              Comment

              • tom_usenet

                #8
                Re: STL-container used with references

                On 06 Apr 2004 11:14:31 EDT, "Howard" <alicebt@hotmai l.com> wrote:
                [color=blue]
                >
                >"tom_usenet " <tom_usenet@hot mail.com> wrote in message[color=green]
                >> ...Your best bet is to use a
                >> container of smart pointers:
                >>
                >> std::vector<sha red_ptr<Element > > vec;
                >> vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));
                >>
                >> See www.boost.org for shared_ptr.
                >>[/color]
                >
                >Just curious...What if you don't have (or want) boost? Is there an STL
                >solution similar to shared_ptr?[/color]

                boost::shared_p tr has been proposed for standardization as part of the
                library technical report. Look out for std::tr1::share d_ptr, coming to
                your compiler soon.

                There is no current standard solution, except to write your own smart
                pointer class (not at all recommended - matching boost::shared_p tr's
                functionality is non-trivial).

                Tom
                --
                C++ FAQ: http://www.parashift.com/c++-faq-lite/
                C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

                Comment

                • tom_usenet

                  #9
                  Re: STL-container used with references

                  On 06 Apr 2004 11:14:31 EDT, "Howard" <alicebt@hotmai l.com> wrote:
                  [color=blue]
                  >
                  >"tom_usenet " <tom_usenet@hot mail.com> wrote in message[color=green]
                  >> ...Your best bet is to use a
                  >> container of smart pointers:
                  >>
                  >> std::vector<sha red_ptr<Element > > vec;
                  >> vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));
                  >>
                  >> See www.boost.org for shared_ptr.
                  >>[/color]
                  >
                  >Just curious...What if you don't have (or want) boost? Is there an STL
                  >solution similar to shared_ptr?[/color]

                  boost::shared_p tr has been proposed for standardization as part of the
                  library technical report. Look out for std::tr1::share d_ptr, coming to
                  your compiler soon.

                  There is no current standard solution, except to write your own smart
                  pointer class (not at all recommended - matching boost::shared_p tr's
                  functionality is non-trivial).

                  Tom
                  --
                  C++ FAQ: http://www.parashift.com/c++-faq-lite/
                  C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

                  Comment

                  • Mike Wahler

                    #10
                    Re: STL-container used with references

                    "tom_usenet " <tom_usenet@hot mail.com> wrote in message
                    news:nel570dc7t i0c7abvc5ebm4eu 3c8uptik8@4ax.c om...[color=blue][color=green]
                    > >Just curious...What if you don't have (or want) boost? Is there an STL
                    > >solution similar to shared_ptr?[/color]
                    >
                    > boost::shared_p tr has been proposed for standardization as part of the
                    > library technical report. Look out for std::tr1::share d_ptr, coming to
                    > your compiler soon.[/color]

                    Curiosity:

                    What's the significance of the (namespace?) name 'tr1'?

                    -Mike



                    Comment

                    • Mike Wahler

                      #11
                      Re: STL-container used with references

                      "tom_usenet " <tom_usenet@hot mail.com> wrote in message
                      news:nel570dc7t i0c7abvc5ebm4eu 3c8uptik8@4ax.c om...[color=blue][color=green]
                      > >Just curious...What if you don't have (or want) boost? Is there an STL
                      > >solution similar to shared_ptr?[/color]
                      >
                      > boost::shared_p tr has been proposed for standardization as part of the
                      > library technical report. Look out for std::tr1::share d_ptr, coming to
                      > your compiler soon.[/color]

                      Curiosity:

                      What's the significance of the (namespace?) name 'tr1'?

                      -Mike



                      Comment

                      • Pete Becker

                        #12
                        Re: STL-container used with references

                        Mike Wahler wrote:[color=blue]
                        >
                        > What's the significance of the (namespace?) name 'tr1'?
                        >[/color]

                        The C++ standards committee has a Technical Report in the works,
                        incorporating recommended library extensions. It's known informally as
                        TR1, and its extensions go in namespace std::tr1.

                        --

                        Pete Becker
                        Dinkumware, Ltd. (http://www.dinkumware.com)

                        Comment

                        • Pete Becker

                          #13
                          Re: STL-container used with references

                          Mike Wahler wrote:[color=blue]
                          >
                          > What's the significance of the (namespace?) name 'tr1'?
                          >[/color]

                          The C++ standards committee has a Technical Report in the works,
                          incorporating recommended library extensions. It's known informally as
                          TR1, and its extensions go in namespace std::tr1.

                          --

                          Pete Becker
                          Dinkumware, Ltd. (http://www.dinkumware.com)

                          Comment

                          • Kevin Goodsell

                            #14
                            Re: STL-container used with references

                            Pete Becker wrote:
                            [color=blue]
                            > Mike Wahler wrote:
                            >[color=green]
                            >>What's the significance of the (namespace?) name 'tr1'?
                            >>[/color]
                            >
                            >
                            > The C++ standards committee has a Technical Report in the works,
                            > incorporating recommended library extensions. It's known informally as
                            > TR1, and its extensions go in namespace std::tr1.
                            >[/color]

                            I don't like the sound of this. Are they going to permanently put these
                            things in std::tr1::? Why wouldn't they just use std::? If they put it
                            in std:: later, will they have to also support it in std::tr1:: for
                            compatibility?

                            -Kevin
                            --
                            My email address is valid, but changes periodically.
                            To contact me please use the address from a recent posting.

                            Comment

                            • Kevin Goodsell

                              #15
                              Re: STL-container used with references

                              Pete Becker wrote:
                              [color=blue]
                              > Mike Wahler wrote:
                              >[color=green]
                              >>What's the significance of the (namespace?) name 'tr1'?
                              >>[/color]
                              >
                              >
                              > The C++ standards committee has a Technical Report in the works,
                              > incorporating recommended library extensions. It's known informally as
                              > TR1, and its extensions go in namespace std::tr1.
                              >[/color]

                              I don't like the sound of this. Are they going to permanently put these
                              things in std::tr1::? Why wouldn't they just use std::? If they put it
                              in std:: later, will they have to also support it in std::tr1:: for
                              compatibility?

                              -Kevin
                              --
                              My email address is valid, but changes periodically.
                              To contact me please use the address from a recent posting.

                              Comment

                              Working...