Member functions of class templates (C++ only)

You may define a template member function outside of its class template definition.

When you call a member function of a class template specialization, the compiler will use the template arguments that you used to generate the class template. The following example demonstrates this:

template<class T> class X {
   public:
      T operator+(T);
};

template<class T> T X<T>::operator+(T arg1) {
   return arg1;
};

int main() {
   X<char> a;
   X<int> b;
   a +'z';
   b + 4;
}

The overloaded addition operator has been defined outside of class X. The statement a + 'z' is equivalent to a.operator+('z'). The statement b + 4 is equivalent to b.operator+(4).

Related information



[ Top of Page | Previous Page | Next Page | Contents | Index ]