Inheriting from std::vector?

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

    #1

    Inheriting from std::vector?

    Hi,

    A colleague has some code like this:

    class CMyObject {
    // Bunch of Member functions
    }

    class CMyObjectList: public std::vector<CMy Object>
    {
    // Bunch of member functions
    }

    I need to inherit the functionality of his object and list as well as add
    some of my own:

    class CMyPersonalObje ct: public CMyObject
    {
    // More stuff
    }

    class CMyPersonalObje ctList: public CMyObjectList
    {
    // More stuff
    }

    But if I pass in CMyPersonalObje ctList to a function, and then try to assign
    the CMyPersonalObje ct to a local variable of that type, I get a compiler
    error telling me that I cannot convert CMyObject to CMyPersonalObje ct... the
    compiler thinks the list I passed in is made of the parent objects.

    What is going on and how do I fix it?

    Thanks,
    B


  • lilburne

    #2
    Re: Inheriting from std::vector?

    BCC wrote:
    [color=blue]
    >
    > But if I pass in CMyPersonalObje ctList to a function, and then try to assign
    > the CMyPersonalObje ct to a local variable of that type, I get a compiler
    > error telling me that I cannot convert CMyObject to CMyPersonalObje ct... the
    > compiler thinks the list I passed in is made of the parent objects.
    >
    > What is going on and how do I fix it?
    >[/color]

    Would need to see the code where the assignment is made.
    However, as std::vector doesn't have a virtual destructor it
    shouldn't really be being used as a base class. That isn't
    your actual problem though.

    Comment

    • Alf P. Steinbach

      #3
      Re: Inheriting from std::vector?

      On Thu, 23 Oct 2003 23:47:14 GMT, "BCC" <bryan@akanta.c om> wrote:
      [color=blue]
      >A colleague has some code like this:
      >
      >class CMyObject {
      > // Bunch of Member functions
      >}[/color]

      Missing semicolon.


      [color=blue]
      >class CMyObjectList: public std::vector<CMy Object>
      >{
      > // Bunch of member functions
      >}[/color]

      Ditto.

      Without further information this inheritance is a little suspicious.

      I can thing of good reasons to inherit from std::string, but not std::vector.



      [color=blue]
      >I need to inherit the functionality of his object and list as well as add
      >some of my own:
      >
      >class CMyPersonalObje ct: public CMyObject
      >{
      > // More stuff
      >}
      >
      >class CMyPersonalObje ctList: public CMyObjectList
      >{
      > // More stuff
      >}
      >
      >But if I pass in CMyPersonalObje ctList to a function, and then try to assign
      >the CMyPersonalObje ct to a local variable of that type,[/color]

      There is no relationship between CMyPersonalObje ctList and CMyPersonalObje ct
      in what you have shown, so the above seems meaningless.


      [color=blue]
      > I get a compiler
      >error telling me that I cannot convert CMyObject to CMyPersonalObje ct...[/color]

      The compiler is right, you cannot do that unless you provide a conversion.


      [color=blue]
      > the compiler thinks the list I passed in is made of the parent objects.[/color]

      Where on earth did you get that absurd idea? Compilers don't think.


      [color=blue]
      >What is going on[/color]

      See e.g. <url: http://news.google.com/>.


      [color=blue]
      >and how do I fix it?[/color]

      Perhaps follow the advice of Michael Moore?

      Comment

      • Grzegorz Sakrejda

        #4
        Re: Inheriting from std::vector?

        On Thu, 23 Oct 2003 23:47:14 GMT, BCC <bryan@akanta.c om> wrote:
        [color=blue]
        > Hi,
        >
        > A colleague has some code like this:
        >
        > class CMyObject {
        > // Bunch of Member functions
        > }
        >
        > class CMyObjectList: public std::vector<CMy Object>
        > {
        > // Bunch of member functions
        > }
        >
        > I need to inherit the functionality of his object and list as well as add
        > some of my own:
        >
        > class CMyPersonalObje ct: public CMyObject
        > {
        > // More stuff
        > }
        >
        > class CMyPersonalObje ctList: public CMyObjectList
        > {
        > // More stuff
        > }
        >
        > But if I pass in CMyPersonalObje ctList to a function, and then try to
        > assign
        > the CMyPersonalObje ct to a local variable of that type, I get a compiler
        > error telling me that I cannot convert CMyObject to CMyPersonalObje ct...
        > the
        > compiler thinks the list I passed in is made of the parent objects.
        >
        > What is going on and how do I fix it?
        >
        > Thanks,
        > B
        >
        >
        >[/color]

        std::vector is a template . CMyObjectList should also be a template, at
        least if you want to have personalobject list.

        template <class T> CObjectList : public std::vector<T> {
        some more
        };

        typedef CObjectList<CMy PersonalObject> CMyPersonalObje ctList;


        --
        grzegorz

        Comment

        • Mike Wahler

          #5
          Re: Inheriting from std::vector?

          "Alf P. Steinbach" <alfps@start.no > wrote in message
          news:3f986ad9.5 08185656@News.C IS.DFN.DE...[color=blue][color=green]
          > > the compiler thinks the list I passed in is made of the parent objects.[/color]
          >
          > Where on earth did you get that absurd idea? Compilers don't think.[/color]

          Understatement of the year. :-)

          -Mike



          Comment

          • Davlet Panech

            #6
            Re: Inheriting from std::vector?


            "BCC" <bryan@akanta.c om> wrote in message
            news:6MZlb.9971 $QQ.83515602@ne wssvr21.news.pr odigy.com...[color=blue]
            > Hi,
            >
            > A colleague has some code like this:
            >
            > class CMyObject {
            > // Bunch of Member functions
            > }
            >
            > class CMyObjectList: public std::vector<CMy Object>
            > {
            > // Bunch of member functions
            > }
            >
            > I need to inherit the functionality of his object and list as well as add
            > some of my own:
            >
            > class CMyPersonalObje ct: public CMyObject
            > {
            > // More stuff
            > }
            >
            > class CMyPersonalObje ctList: public CMyObjectList
            > {
            > // More stuff
            > }
            >
            > But if I pass in CMyPersonalObje ctList to a function, and then try to[/color]
            assign[color=blue]
            > the CMyPersonalObje ct to a local variable of that type, I get a compiler
            > error telling me that I cannot convert CMyObject to CMyPersonalObje ct...[/color]
            the[color=blue]
            > compiler thinks the list I passed in is made of the parent objects.[/color]

            It won't work, because the vector contains CMyObjects, not
            CMyPersonalObje cts. So whenever you insert a CMyPersonalObje ct in that
            vector, it'll get "truncated" to it's base type. You might be able to use a
            vector of pointers to (dynamically-allocated) CMyObjects (+derivatives)
            instead, but you'll run into other problems:
            - Manual memory allocation is difficult and error-prone (but "smart pointer"
            libraries might help, see for example www.boost.org )
            - You'll have to downcast the stored addresses all the time
            - I think it is wrong to have CMyPersonalObje ctList as a subclass of
            CMyObjectList, because such hierarchy permits insertions of CMyObjects into
            CMyPersonalObje ctList (you might want to do a search on "Liskov Substitution
            Principle" to learn why this is a bad idea. For example read this article:
            http://www.brent.worden.org/articles...rinciple.html).

            What exactly are you trying to do? Maybe there's a better way to do it.
            [color=blue]
            >
            > What is going on and how do I fix it?
            >
            > Thanks,
            > B
            >
            >[/color]


            Comment

            • David Rubin

              #7
              Re: Inheriting from std::vector?

              Mike Wahler wrote:[color=blue]
              > "Alf P. Steinbach" <alfps@start.no > wrote in message
              > news:3f986ad9.5 08185656@News.C IS.DFN.DE...
              >[color=green][color=darkred]
              >>>the compiler thinks the list I passed in is made of the parent objects.[/color]
              >>
              >>Where on earth did you get that absurd idea? Compilers don't think.[/color]
              >
              >
              > Understatement of the year. :-)[/color]

              OP must have confused "The Compiler That Thinks" with "The Thinking
              Man's Compiler."

              /david :-)

              --
              "As a scientist, Throckmorton knew that if he were ever to break wind in
              the echo chamber, he would never hear the end of it."

              Comment

              • Rolf Magnus

                #8
                Re: Inheriting from std::vector?

                lilburne wrote:
                [color=blue]
                > However, as std::vector doesn't have a virtual destructor it
                > shouldn't really be being used as a base class.[/color]

                There is no problem with using it as a base class as long as you don't
                destroy the object through a pointer to std::vector.

                Comment

                • lilburne

                  #9
                  Re: Inheriting from std::vector?

                  Rolf Magnus wrote:
                  [color=blue]
                  > lilburne wrote:
                  >
                  >[color=green]
                  >>However, as std::vector doesn't have a virtual destructor it
                  >>shouldn't really be being used as a base class.[/color]
                  >
                  >
                  > There is no problem with using it as a base class as long as you don't
                  > destroy the object through a pointer to std::vector.
                  >[/color]

                  And you ensure that how?

                  Comment

                  • Pete Becker

                    #10
                    Re: Inheriting from std::vector?

                    lilburne wrote:[color=blue]
                    >
                    > Rolf Magnus wrote:
                    >[color=green]
                    > > lilburne wrote:
                    > >
                    > >[color=darkred]
                    > >>However, as std::vector doesn't have a virtual destructor it
                    > >>shouldn't really be being used as a base class.[/color]
                    > >
                    > >
                    > > There is no problem with using it as a base class as long as you don't
                    > > destroy the object through a pointer to std::vector.
                    > >[/color]
                    >
                    > And you ensure that how?[/color]

                    You don't do it.

                    --

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

                    Comment

                    • lilburne

                      #11
                      Re: Inheriting from std::vector?

                      Pete Becker wrote:
                      [color=blue]
                      > lilburne wrote:
                      >[color=green]
                      >>Rolf Magnus wrote:
                      >>
                      >>[color=darkred]
                      >>>lilburne wrote:
                      >>>
                      >>>
                      >>>
                      >>>>However, as std::vector doesn't have a virtual destructor it
                      >>>>shouldn't really be being used as a base class.
                      >>>
                      >>>
                      >>>There is no problem with using it as a base class as long as you don't
                      >>>destroy the object through a pointer to std::vector.
                      >>>[/color]
                      >>
                      >>And you ensure that how?[/color]
                      >
                      >
                      > You don't do it.
                      >[/color]

                      I take it you mean: you don't inherit from a class that
                      doesn't contain a virtual destructor, not that you don't
                      delete a pointer to a class that doesn't have a virtual
                      destructor.

                      Comment

                      • Pete Becker

                        #12
                        Re: Inheriting from std::vector?

                        lilburne wrote:[color=blue]
                        >
                        > Pete Becker wrote:
                        >[color=green]
                        > > lilburne wrote:
                        > >[color=darkred]
                        > >>Rolf Magnus wrote:
                        > >>
                        > >>
                        > >>>lilburne wrote:
                        > >>>
                        > >>>
                        > >>>
                        > >>>>However, as std::vector doesn't have a virtual destructor it
                        > >>>>shouldn't really be being used as a base class.
                        > >>>
                        > >>>
                        > >>>There is no problem with using it as a base class as long as you don't
                        > >>>destroy the object through a pointer to std::vector.
                        > >>>
                        > >>
                        > >>And you ensure that how?[/color]
                        > >
                        > >
                        > > You don't do it.
                        > >[/color]
                        >
                        > I take it you mean: you don't inherit from a class that
                        > doesn't contain a virtual destructor, not that you don't
                        > delete a pointer to a class that doesn't have a virtual
                        > destructor.[/color]

                        You take it wrong. Pay attention to context.

                        --

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

                        Comment

                        • lilburne

                          #13
                          Re: Inheriting from std::vector?

                          Pete Becker wrote:
                          [color=blue]
                          > lilburne wrote:
                          >[color=green]
                          >>Pete Becker wrote:
                          >>
                          >>[color=darkred]
                          >>>lilburne wrote:
                          >>>
                          >>>
                          >>>>Rolf Magnus wrote:
                          >>>>
                          >>>>
                          >>>>
                          >>>>>lilburne wrote:
                          >>>>>
                          >>>>>
                          >>>>>
                          >>>>>
                          >>>>>>However , as std::vector doesn't have a virtual destructor it
                          >>>>>>shouldn 't really be being used as a base class.
                          >>>>>
                          >>>>>
                          >>>>>There is no problem with using it as a base class as long as you don't
                          >>>>>destroy the object through a pointer to std::vector.
                          >>>>>
                          >>>>
                          >>>>And you ensure that how?
                          >>>
                          >>>
                          >>>You don't do it.
                          >>>[/color]
                          >>
                          >>I take it you mean: you don't inherit from a class that
                          >>doesn't contain a virtual destructor, not that you don't
                          >>delete a pointer to a class that doesn't have a virtual
                          >>destructor.[/color]
                          >
                          >
                          > You take it wrong. Pay attention to context.
                          >[/color]

                          Amazing:

                          #include <iostream>
                          #include <memory>

                          using namespace std;

                          class A {
                          public:
                          A();
                          ~A();
                          };

                          class B : public A {
                          string s;
                          public:
                          B();
                          ~B();
                          };

                          A::A() { cout << "A::A" << endl;}
                          A::~A() { cout << "A::~A" << endl;}
                          B::B() { cout << "B::B" << endl;}
                          B::~B() { cout << "B::~B" << endl;}

                          int main()
                          {
                          auto_ptr<A> a(new B);
                          return 0;
                          }



                          Comment

                          • Pete Becker

                            #14
                            Re: Inheriting from std::vector?

                            lilburne wrote:[color=blue]
                            >[color=green]
                            > > You take it wrong. Pay attention to context.
                            > >[/color]
                            >
                            > Amazing:
                            >[/color]

                            You wrote bad code, having been told it's bad. What's your point?

                            --

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

                            Comment

                            • lilburne

                              #15
                              Re: Inheriting from std::vector?

                              Pete Becker wrote:
                              [color=blue]
                              > lilburne wrote:
                              >[color=green][color=darkred]
                              >>>You take it wrong. Pay attention to context.
                              >>>[/color]
                              >>
                              >>Amazing:
                              >>[/color]
                              >
                              >
                              > You wrote bad code, having been told it's bad. What's your point?
                              >[/color]

                              What was bad about it?

                              Comment

                              Working...