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);};


You may define the template member function inside the class body, as in:

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


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;}


You call the template member as follows:

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

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist