inheritance and templates

This is a discussion on inheritance and templates within the C++ Programming forums, part of the General Programming Boards category; i cannot find one good example...any ideas?...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    inheritance and templates

    i cannot find one good example...any ideas?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Remember that a class has to inherit from a fully defined type, so the follwing are valid:
    Code:
    template<class T>
    struct A
    {
    };
    
    template<class T>
    struct B : public A<T>
    {
    };
    
    struct C : public A<int>
    {
    };
    The following is invalid.
    Code:
    template<class T>
    struct A
    {
    };
    
    struct B : public A
    {
    };

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I highly recommend C++ Templates: The Complete Guide
    by David Vandevoorde and Nicolai M. Josuttis.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    You can also do
    Code:
    template<typename T>
    class A : public T {  };

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    interface class is a composite of inherited class (inherited class is data mem of interface).
    inherited class inherits base class (which is not template, however may be ANY class).
    intefarce class doesn't necessarily need to be a template, except that one of it's data members (inherited class) is a template.

    anyway..is this right?

    Code:
    class Base //not template 
    {
             
    };
    
    template<class T>  //T will be Base (for this example, although base could be ANY class)
    class InheritedClass : public T
    {
           
    
    };
    
    template<class T>
    class InterfaceClass
    {
            InheritedClass<T> *ptr;   //is this correct?
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. templates and inheritance
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2004, 04:34 PM
  2. Dilemma with templates and inheritance..!
    By Halloko in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2004, 02:01 PM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 03:46 AM
  4. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  5. Mixing templates and inheritance, and the damage caused ;)
    By SilentStrike in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21