When I want to define a method declared in a template class, but that the method doesn't depend on the template parameters, have I to define it in the include files as :
template class<typename T>
void MyClass::myMethod()
{
...
}
or may I define it in the cpp file as :
void MyClass::myMethod()
{
...
}
?
Thanks.
A non-template class can have template member functions, if required. Notice the syntax. Unlike a member function for a template class, a template member function is just like a free template function but scoped to its containing class.
Deriving from a non-template base class There is no requirement that your base class be a template. It is quite possible to have a template class inherit from a 'normal' class. This mechanism is recommended if your template class has a lot of non-template attributes and operations.
Template non-type arguments in C++It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.
A function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.
You'll need to define your method like this:
template class<typename T>
void MyClass<T>::myMethod()
{
// Method Body
}
The reason for this is that the method actually is dependent on the template parameter. Remember that every method has access to the special variable this
; during the method call this
is actually a parameter passed to the method. this
changes in type depending on the template parameter specified during object instantiation and, as such, all methods must be template methods in order to accommodate all forms of this
.
Well, if the method doesn't depend on template parameter, you can only do that with inheritance AFAIK.
The downside: more code + inheritance
The upside: (much) less code generated, depending on what parts of your code actually are template dependent. In the below example, method NonDependentMethod
will only generate one assembly while DependentMethod
will generate as many as there are different template parameters (just one in this case, but make a MyClass<float>
and you have two, etc).
#include <iostream>
using namespace std;
class MyClassBase
{
public:
void NonDependentMethod();
};
template <class T> class MyClass : public MyClassBase
{
public:
void DependentMethod(T param);
};
void MyClassBase::NonDependentMethod()
{
cout << "NonDependentMethod" << endl;
}
template<class T> void MyClass<T>::DependentMethod(T param)
{
cout << "DependentMethod " << param << endl;
}
int main() {
// your code goes here
MyClass<int> cls;
cls.NonDependentMethod();
cls.DependentMethod(2);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With