devxlogo

Using a Template Member Function

Using a Template Member Function

An ordinary class may have template member functions. In the following example, the class A declares a template member function called f():

 class A{public: template  T f(T t);};

size=3>
You may define the template member function inside the class body, as in:

 class A{public: template  T f(T t) { return t;}};

size=3>
Alternatively, you may define it outside the class body. It’s advisable to define the template member function in the same file that contains the class declaration:

 class A{public: template  T f(T t); // declaration};template  T A::f(T t) // definition{ return t;}

size=3>
You call the template member as follows:

 A a;int num=a.f(5);

size=3>

devx-admin

Share the Post: