how to extend a template class?

This is a discussion on how to extend a template class? within the C++ Programming forums, part of the General Programming Boards category; Say, class A is a template class and I want to create a class B which extends class A. What ...

  1. #1
    dkt
    dkt is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    18

    Question how to extend a template class?

    Say, class A is a template class and I want to create a class B which extends class A. What is the correct syntax to do this? I can't find this from the books..

    Thank you in advance.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    It depends what you're trying to do. A very basic example -

    Code:
    template <class T>
    struct base
    {
    	T var;
    };
    
    template <class R,class T>
    struct derived : public base<R>
    {
    	T var2;
    };
    
    
    int main () 
    {
    	derived<int,double> d;
    	d.var=1;
    	d.var2=2.;
    
    	return 0;     
    
    }

  3. #3
    dkt
    dkt is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    18
    Thanks for reply.

    I got another problem, I need to use the .h and .cpp, not only .cpp

    I now has something like this, but got error. You can get the defination of QList in here:
    http://doc.trolltech.com/2.3/qlist-h.html
    And the QGList in here:
    http://doc.trolltech.com/2.3/qglist-h.html

    chain.h:

    #include <qlist.h>
    template<class T> class Chain: public QList <T> {
    public:
    Chain();
    };

    chain.cpp:
    #include <chain.h>
    template <class T> Chain::Chain() {}
    Last edited by dkt; 03-03-2002 at 08:23 AM.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    The definitions of template methods, must be visible in the same file that they are declared in.

  5. #5
    Unregistered
    Guest
    If you really want to split dec and def
    then..
    //temp.h

    #ifndef temp_h
    #define temp_h

    // template <class...

    #include temp.cpp

    #endif

    ...and in temp.cpp...

    #ifndef temp_cpp
    #define temp_cpp

    // members of template

    #endif

  6. #6
    Unregistered
    Guest
    Originally posted by Unregistered
    If you really want to split dec and def
    then..
    //temp.h

    #ifndef temp_h
    #define temp_h

    // template <class...

    #include temp.cpp

    #endif

    ...and in temp.cpp...

    #ifndef temp_cpp
    #define temp_cpp

    #include temp.h

    // members of template

    #endif

  7. #7
    dkt
    dkt is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    18
    i find out the problem in the .cpp file:

    chain.cpp:
    #include <chain.h>
    template <class T> Chain <T>::Chain() {}
    // not: template <class T> Chain::Chain() {}

    template <class T> Chain <T>::~Chain() {}

  8. #8
    dkt
    dkt is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    18
    Yes, this is very important.
    There's no point in spiliting the template class into .h and .cpp. I found out that other classes cannot use the template class (compile error) when the template is spilted.

    Originally posted by Sorensen
    The definitions of template methods, must be visible in the same file that they are declared in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 05:50 PM
  3. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  4. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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