container of pointers and virtual member function templates

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

    container of pointers and virtual member function templates

    Hi,

    I have a simplified version of a problem that I am facing (hope I haven't
    oversimplified it). This code doesn't work now and I want to find how I can
    make it work. Can I call the derived class version from the base class
    pointer and still be able to use a vector with derived element pointers?

    class BaseElem { };

    class DerivedElem1 { };

    class DerivedElem2 { };

    class BaseContainer
    {
    public:
    virtual void GetAllElems(std ::vector<BaseEl em*>& allElems) const;
    };

    class DerivedContaine r1
    {
    public:
    virtual void GetAllElems(std ::vector<Derive dElem1*>& allElems) const;
    };

    class DerivedContaine r2
    {
    public:
    virtual void GetAllElems(std ::vector<Derive dElem2*>& allElems) const;
    };


  • David White

    #2
    Re: container of pointers and virtual member function templates

    "Sat" <remove_this_hs itas13@hotmail. com> wrote in message
    news:bou5if$vgd $1@news01.intel .com...[color=blue]
    > Hi,
    >
    > I have a simplified version of a problem that I am facing (hope I haven't
    > oversimplified it). This code doesn't work now and I want to find how I[/color]
    can[color=blue]
    > make it work. Can I call the derived class version from the base class
    > pointer and still be able to use a vector with derived element pointers?
    >
    > class BaseElem { };
    >
    > class DerivedElem1 { };
    >
    > class DerivedElem2 { };[/color]

    You haven't derived either of the last two classes from the first one.
    [color=blue]
    > class BaseContainer
    > {
    > public:
    > virtual void GetAllElems(std ::vector<BaseEl em*>& allElems) const;
    > };
    >
    > class DerivedContaine r1
    > {
    > public:
    > virtual void GetAllElems(std ::vector<Derive dElem1*>& allElems) const;
    > };
    >
    > class DerivedContaine r2
    > {
    > public:
    > virtual void GetAllElems(std ::vector<Derive dElem2*>& allElems) const;
    > };[/color]

    Again, neither of your "derived" containers derives from your base
    container. In fact, you have no inheritance anywhere in this code.

    Even if you specify the inheritance that you appear to have intended for
    both elements and containers, your derived-class virtual functions do not
    override the base-class virtual function. A vector<BaseElem *> is a type
    unrelated to vector<DerivedE lem1*>, so if I've understood your question
    correctly the answer is "no".

    DW



    Comment

    • Sat

      #3
      Re: container of pointers and virtual member function templates

      Thanks David, You understood the question correctly. The inheritance part
      was missing in my code.

      I have re-written it here:
      class BaseElem { };

      class DerivedElem1 : public BaseElem { };

      class DerivedElem2 : public BaseElem { };

      class BaseContainer
      {
      public:
      virtual void GetAllElems(std ::vector<BaseEl em*>& allElems) const;
      };

      class DerivedContaine r1 : public BaseContainer
      {
      public:
      virtual void GetAllElems(std ::vector<Derive dElem1*>& allElems) const;
      };

      class DerivedContaine r2 : public BaseContainer
      {
      public:
      virtual void GetAllElems(std ::vector<Derive dElem2*>& allElems) const;
      };

      I understand that the std::vector<Der ivedElem*> is unrelated to
      std::vector<Bas eElem*>. I am trying to see if anyone has a good idea of
      implementing this in a different way. The intent is to return a bunch of
      DerivedElem pointers in a container using a virtual function.

      I tried making GetAllElems a templated member function like this:

      template<ElemTy pe>
      virtual void GetAllElems(std ::vector<ElemTy pe>& allElems) const;

      but later found that templated member functions cannot be virtual functions.
      That's why I am stuck.

      Thanks
      Sat

      "David White" <no@email.provi ded> wrote in message
      news:GoEsb.1225 $xm.62046@nasal .pacific.net.au ...[color=blue]
      > "Sat" <remove_this_hs itas13@hotmail. com> wrote in message
      > news:bou5if$vgd $1@news01.intel .com...[color=green]
      > > Hi,
      > >
      > > I have a simplified version of a problem that I am facing (hope I[/color][/color]
      haven't[color=blue][color=green]
      > > oversimplified it). This code doesn't work now and I want to find how I[/color]
      > can[color=green]
      > > make it work. Can I call the derived class version from the base class
      > > pointer and still be able to use a vector with derived element pointers?
      > >
      > > class BaseElem { };
      > >
      > > class DerivedElem1 { };
      > >
      > > class DerivedElem2 { };[/color]
      >
      > You haven't derived either of the last two classes from the first one.
      >[color=green]
      > > class BaseContainer
      > > {
      > > public:
      > > virtual void GetAllElems(std ::vector<BaseEl em*>& allElems) const;
      > > };
      > >
      > > class DerivedContaine r1
      > > {
      > > public:
      > > virtual void GetAllElems(std ::vector<Derive dElem1*>& allElems)[/color][/color]
      const;[color=blue][color=green]
      > > };
      > >
      > > class DerivedContaine r2
      > > {
      > > public:
      > > virtual void GetAllElems(std ::vector<Derive dElem2*>& allElems)[/color][/color]
      const;[color=blue][color=green]
      > > };[/color]
      >
      > Again, neither of your "derived" containers derives from your base
      > container. In fact, you have no inheritance anywhere in this code.
      >
      > Even if you specify the inheritance that you appear to have intended for
      > both elements and containers, your derived-class virtual functions do not
      > override the base-class virtual function. A vector<BaseElem *> is a type
      > unrelated to vector<DerivedE lem1*>, so if I've understood your question
      > correctly the answer is "no".
      >
      > DW
      >
      >
      >[/color]


      Comment

      • David White

        #4
        Re: container of pointers and virtual member function templates

        "Sat" <remove_this_hs itas13@hotmail. com> wrote in message
        news:bp0cri$uuh $1@news01.intel .com...[color=blue]
        > I understand that the std::vector<Der ivedElem*> is unrelated to
        > std::vector<Bas eElem*>. I am trying to see if anyone has a good idea of
        > implementing this in a different way. The intent is to return a bunch of
        > DerivedElem pointers in a container using a virtual function.
        >
        > I tried making GetAllElems a templated member function like this:
        >
        > template<ElemTy pe>
        > virtual void GetAllElems(std ::vector<ElemTy pe>& allElems) const;
        >
        > but later found that templated member functions cannot be virtual[/color]
        functions.

        That's right, probably because of implementation difficulties for the
        compiler in constructing a v-table that can support templates.
        [color=blue]
        > That's why I am stuck.[/color]

        I can't think of any really nice way to do it. The best I can come up with
        is a virtual function that takes a std::vector<Bas eElem*> &, and a
        non-virtual member function template in the base class that calls the
        virtual function and converts the vector it creates into another vector of
        the correct type of element using a cast. Or, if you want to ensure type
        safety at the expense of execution time, you can dynamic_cast every element
        separately.

        Another, even nastier, method - but fast - would be to directly cast the
        std::vector<Der ivedElem*> & passed to the template member in the base class
        into a std::vector<Bas eElem*> & for passing to the virtual function.

        I probably need a few slaps across the face from other regulars here or I
        might come up with something even worse :-)

        DW



        Comment

        • David White

          #5
          Re: container of pointers and virtual member function templates

          "David White" <no@email.provi ded> wrote in message
          news:hzUsb.1260 $xm.64686@nasal .pacific.net.au ...[color=blue]
          >
          > I can't think of any really nice way to do it. The best I can come up with
          > is a virtual function that takes a std::vector<Bas eElem*> &, and a
          > non-virtual member function template in the base class that calls the
          > virtual function and converts the vector it creates into another vector of
          > the correct type of element using a cast.[/color]

          To clarify, here I meant casting the address of the first element of
          std::vector<Bas eElem*>, which is a BaseElem**, into a DerivedElem** for
          insertion into the std::vector<Der ivedElem*> & passed to the template
          function. I didn't mean casting one vector type into another, as it
          appeared. (Though I meant exactly that later on.)

          DW



          Comment

          Working...