O'Reilly logo

Mastering C++ Programming by Jeganathan Swaminathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Code walkthrough

The following code declares a class template. The keyword, template <typename T, int size>, can be replaced with <class T, int size>. Both keywords can be interchanged in function and class templates; however, as an industry best practice, template<class T> can be used only with class templates to avoid confusion:

template <typename T, int size>class MyAlgorithm 

One of the overloaded sort() methods is defined inline as follows:

 void sort( array<T, size> &data ) {      for ( int i=0; i<size; ++i ) {          for ( int j=0; j<size; ++j ) {              if ( data[i] < data[j] )                 swap ( data[i], data[j] );          }      } } 

The second overloaded sort() function is just declared within the class scope, without any definition, as follows:

template <typename T, int ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required