Friday, June 13, 2008

C++ member template function

With Visual Studio 2003 you can use member template functions, for example:

class Foo {

    template <class T> void bar(T* t) {}

};

that you can call in this way:

void main() {
  int a = 10;
  Foo obj;
  obj.bar(a);

}

Make sure that when you declare the bar method you include also the method definition otherwise you will end-up with a linker error.

1 comment:

carte sdhc 32gb said...

C++ Function templates are those functions which can handle different data types without separate code for each of them. For a similar operation on several kinds of data types, a programmer need not write different versions by overloading a function. It is enough if he writes a C++ template based function. This will take care of all the data types.