Defining Member Function Templates
Explore how to define member function templates in both nontemplate classes and class templates. Understand differences between class template member functions and member function templates, including template parameter deduction and usage. Gain practical knowledge on casting with member function templates in C++.
We'll cover the following...
So far, we have learned about function templates and class templates. It’s possible to define member function templates, too, in both nontemplate classes and class templates. In this lesson, we’ll learn how to do this. To understand the differences, let’s start with the following example:
template<typename T>class composition{public:T add(T const a, T const b){return a + b;}};
The composition class is a class template. It has a single member function called add that uses the type parameter T. This class can be used as follows:
We first need to instantiate an object of the composition class. Notice that we must explicitly specify the argument ...