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.
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.
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; }
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 09:23 AM.
The definitions of template methods, must be visible in the same file that they are declared in.
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
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
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() {}
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.