But isn't that the same for any non-template class that I define in a separate .cpp file
No, as long as you compile and link methods from the "separate .cpp file", compiler can "look" into compiled code and take any method it needs. In the template case, there is no code in separate .cpp generated for the specific template instantiation.
So the declarations are available at compile time, right ?
Declarations - yes, definitions - no.
But I still don't understand why there is a difference to a non-template class.
Because when you create non-template class derived from QVector<Type>, compiler has everything it needs - QVector<Type> is instantiated, so its like a normal class from now on.
Main difference is in the .cpp files -
Case 1: template-based approach:
// .cpp file with template methods:
template <class T> SSVector<T>::SSVector()
{}
template <class T> SSVector<T>::SSVector(const SSVector<T>& that)
{}
// compiler has a template, but when you compile this file, it does not "see" that
// somewhere in other files you want to use above methods for a specific T
// .cpp file with template methods:
template <class T> SSVector<T>::SSVector()
{}
template <class T> SSVector<T>::SSVector(const SSVector<T>& that)
{}
// compiler has a template, but when you compile this file, it does not "see" that
// somewhere in other files you want to use above methods for a specific T
To copy to clipboard, switch view to plain text mode
Case 2: non-template approach:
// .cpp file with non-template methods
#include "ssvector.h"
// include actually pastes the file content, so it looks like:
class SSVector : public QVector<int/*or anything else*/>
{
public:
SSVector();
SSVector(const SSVector& that);
};
SSVector::SSVector()
{}
SSVector(const SSVector& that)
{}
// all is ok, compiler knows what is QVector<int>, and it has both declarations and
// definitions of all needed SSVector methods
// .cpp file with non-template methods
#include "ssvector.h"
// include actually pastes the file content, so it looks like:
class SSVector : public QVector<int/*or anything else*/>
{
public:
SSVector();
SSVector(const SSVector& that);
};
SSVector::SSVector()
{}
SSVector(const SSVector& that)
{}
// all is ok, compiler knows what is QVector<int>, and it has both declarations and
// definitions of all needed SSVector methods
To copy to clipboard, switch view to plain text mode
I hope it helps.
Bookmarks