1

For example

class A {
 public:
   void fun(Array a);
   void fun(Vector a);
   /* Most codes in these two functions are same. */
   /* Can certainly be merged into a template function if they were not member functions. */
}

Please note that I hope to have both these two versions of fun() in class A. Thanks.

updogliu
  • 6,066
  • 7
  • 37
  • 50

3 Answers3

2

Even if the class is not templated itself you can write a member function that is templated in the same manner that you would write a templated function that was not a method of a class.

template <class myType >
myType func (myType a) {
 /* do something */;
}
Matt
  • 1,513
  • 3
  • 16
  • 32
0

Yes it is possible to create template member function like normal function. Just keep the code generic like it can work in both the situations involving vector and other data types.

template <typename T>
void fun(T var) {}
Daemon
  • 1,575
  • 1
  • 17
  • 37
0

Yes it is possible, see this SO question.

Community
  • 1
  • 1
Robotnik
  • 3,643
  • 3
  • 31
  • 49