derivation of a template class

Discussion in 'C++' started by maadhuu, May 17, 2005.

  1. maadhuu

    maadhuu Guest

    hello,
    i want to know whether one can derive from a template class where the
    derived class is also a template class....if this is true, then if i have
    2 functions say f() in the parent class and g() in the derived class ,
    where the function f() is called,then it gives an error ..why??
    maadhuu, May 17, 2005
    #1
    1. Advertising

  2. * maadhuu:
    > i want to know whether one can derive from a template class where the
    > derived class is also a template class....


    Yes; what were you thinking of might be in the way?

    template< typename T > class A {};
    template< typename T > class B: public A<T> {};


    >if this is true, then if i have
    > 2 functions say f() in the parent class and g() in the derived class ,
    > where the function f() is called,then it gives an error ..why??


    Post the relevant code.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 17, 2005
    #2
    1. Advertising

  3. maadhuu

    maadhuu Guest

    template<typename T>
    class B {
    public:
    void f() { }
    };

    template<typename T>
    class D : public B<T> {
    public:
    void g()
    {
    f(); // compiler gives an error here
    }
    };
    this is the way it works......my question is whether this is allowed at
    all i.e. deriving from a template class
    maadhuu, May 17, 2005
    #3
  4. * maadhuu:
    > template<typename T>
    > class B {
    > public:
    > void f() { }
    > };
    >
    > template<typename T>
    > class D : public B<T> {
    > public:
    > void g()
    > {
    > f(); // compiler gives an error here
    > }
    > };
    > this is the way it works......my question is whether this is allowed at
    > all i.e. deriving from a template class


    Yes.

    You just have to inform the compiler that f is an inherited member routine.

    One way is to employ a 'using' directive in class D, another way is to
    qualify the call with 'this->'.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 17, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    12
    Views:
    1,682
    Jonathan Turkanis
    Aug 20, 2004
  2. christopher diggins
    Replies:
    16
    Views:
    724
    Pete Becker
    May 4, 2005
  3. Pierre Rouleau
    Replies:
    2
    Views:
    379
    Pierre Rouleau
    Apr 25, 2005
  4. Massimo Soricetti
    Replies:
    6
    Views:
    459
    Daniel T.
    Feb 2, 2006
  5. none
    Replies:
    6
    Views:
    462
    Chris Mellon
    Oct 12, 2007
Loading...

Share This Page