template class derived from non-template base class

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

    template class derived from non-template base class

    ok, I have 2 classes roughly like these 2:

    class A; // various data classes to be used in D

    class B {
    public:
    void x() { y() }
    protected:
    virtual void y();
    };

    template < class T >
    class D : public B {
    public:
    void z();
    protected:
    virtual void y();
    };

    class D< A > d1;

    Now, whenever I call d1.x(), it runs B::y() instead of D::y().

    Is deriving a template class from a class without templates forbidden
    in C++?
    Is it possibly a compiler support issue?

    My reason for doing this was to keep the size from getting so big as
    I'm using the D class with many different classes in place of A on a
    device with limited memory and storage.

    Thanks,
    Matt Graham
  • Victor Bazarov

    #2
    Re: template class derived from non-template base class

    "Matt Graham" <mgraham@cymedi x.com> wrote...[color=blue]
    > ok, I have 2 classes roughly like these 2:
    >
    > class A; // various data classes to be used in D
    >
    > class B {
    > public:
    > void x() { y() }
    > protected:
    > virtual void y();
    > };
    >
    > template < class T >
    > class D : public B {
    > public:
    > void z();
    > protected:
    > virtual void y();
    > };
    >
    > class D< A > d1;
    >
    > Now, whenever I call d1.x(), it runs B::y() instead of D::y().[/color]

    Posting non-compilable code does not get you anywhere. Here
    is what I came up with (after fixing your code and adding the
    necessary framework):

    ---------------------------------
    #include <iostream>
    using namespace std;

    class B {
    public:
    void x() { y(); }
    protected:
    virtual void y() { cout << "B::y\n"; }
    };

    template < class T >
    class D : public B {
    public:
    void z();
    protected:
    virtual void y() { cout << "D::y\n"; }
    };

    int main()
    {
    D<int> d1;
    d1.x();

    return 0;
    }
    ---------------------------------
    The output of this code is
    D::y

    So, either you forgot to actually declare 'y' virtual in
    your original code, or there is something else. Please post
    real code next time.
    [color=blue]
    >
    > Is deriving a template class from a class without templates forbidden
    > in C++?[/color]

    No.
    [color=blue]
    > Is it possibly a compiler support issue?[/color]

    No, it shouldn't be. If it's so, change your compiler.
    [color=blue]
    > My reason for doing this was to keep the size from getting so big as
    > I'm using the D class with many different classes in place of A on a
    > device with limited memory and storage.[/color]

    That's of no real consequence.

    Victor


    Comment

    • Gianni Mariani

      #3
      Re: template class derived from non-template base class

      Matt Graham wrote:[color=blue]
      > ok, I have 2 classes roughly like these 2:
      >
      > class A; // various data classes to be used in D
      >
      > class B {
      > public:
      > void x() { y() }
      > protected:
      > virtual void y();
      > };
      >
      > template < class T >
      > class D : public B {
      > public:
      > void z();
      > protected:
      > virtual void y();
      > };
      >
      > class D< A > d1;
      >
      > Now, whenever I call d1.x(), it runs B::y() instead of D::y().
      >
      > Is deriving a template class from a class without templates forbidden
      > in C++?
      > Is it possibly a compiler support issue?
      >
      > My reason for doing this was to keep the size from getting so big as
      > I'm using the D class with many different classes in place of A on a
      > device with limited memory and storage.
      >
      > Thanks,
      > Matt Graham[/color]

      I suggest that next time you post an actual working example of the code.

      #include <iostream>

      class A {
      };

      class B {
      public:
      void x() { y(); }
      protected:
      virtual void y() { std::cout << "B::y\n"; }
      };



      template < class T >
      class D : public B {
      public:
      void z();
      protected:
      virtual void y() { std::cout << "D<>::y\n"; }
      };

      class D< A > d1;


      int main()
      {
      d1.x();
      }


      anyhow:

      The output for gcc 3.3 is:
      D<>::y

      which seems correct to me and reading the rest of your post indicates
      that this is not what you got but what you expect.

      Sounds like compiler trubbel.





      Comment

      • Chris Theis

        #4
        Re: template class derived from non-template base class


        "Matt Graham" <mgraham@cymedi x.com> wrote in message
        news:28763143.0 307170721.7fcf3 cd7@posting.goo gle.com...[color=blue]
        > ok, I have 2 classes roughly like these 2:
        >
        > class A; // various data classes to be used in D
        >
        > class B {
        > public:
        > void x() { y() }[/color]

        I guess you forgot the semicolon here.
        [color=blue]
        > protected:
        > virtual void y();
        > };
        >
        > template < class T >
        > class D : public B {
        > public:
        > void z();
        > protected:
        > virtual void y();
        > };
        >
        > class D< A > d1;[/color]

        I assume that you meant

        D<A> d1;
        [color=blue]
        > Now, whenever I call d1.x(), it runs B::y() instead of D::y().
        >
        > Is deriving a template class from a class without templates forbidden
        > in C++?[/color]

        No, this is perfectly legal. In principle your code should run just fine.

        [color=blue]
        > Is it possibly a compiler support issue?
        >[/color]
        What compiler are you using and can you post some more code.
        [color=blue]
        > My reason for doing this was to keep the size from getting so big as
        > I'm using the D class with many different classes in place of A on a
        > device with limited memory and storage.
        >
        > Thanks,
        > Matt Graham[/color]

        Chris


        Comment

        • Matt Graham

          #5
          Re: template class derived from non-template base class

          "Chris Theis" <Christian.Thei s@nospam.cern.c h> wrote in message news:<bf6jk1$ku 9$1@sunnews.cer n.ch>...[color=blue]
          > "Matt Graham" <mgraham@cymedi x.com> wrote in message
          > news:28763143.0 307170721.7fcf3 cd7@posting.goo gle.com...
          >[color=green]
          > > Now, whenever I call d1.x(), it runs B::y() instead of D::y().
          > >
          > > Is deriving a template class from a class without templates forbidden
          > > in C++?[/color]
          >
          > No, this is perfectly legal. In principle your code should run just fine.
          >
          >[color=green]
          > > Is it possibly a compiler support issue?
          > >[/color]
          > What compiler are you using and can you post some more code.[/color]


          Thanks to those who responded. I decided it must be a compiler issue
          and ended up trying a few work arounds before finding one that I
          should have tried some time ago.

          Sorry about posting code that couldn't be compiled. I'll note that for
          next time and not be so concerned about brievity. My actual code
          compiled fine and ran fine right up until it called the base class
          virtual member function instead of the derived class virtual member.

          The compiler I am using is CodeWarrior 9 for PalmOS. I guess I'll
          check out a related newsgroup to see if that's a known problem.

          Thanks
          Matt

          Comment

          Working...