0

I have a question that confuses me. I know what is template and aim of it but I have some blank points on usage.

I have a Template class like that:

template <class elemType>
class SSorting {
public:

int seqSearch(const elemType list[], int length, const elemType& item);
};

When I code the member of a class function seqSearch I need to declare as:

template <class elemType>
int SSorting <elemType> :: seqSearch(const elemType list[], int length, const elemType& item){
    *statements*
}

Everything okay at this point. template says this is a function that inside a template class and int in front of the function means that the function will return an integer value, but the part that I don't understand is why we have to write SSorting <elemType> :: seqSearch instead of SSorting :: seqSearch. I already say this is a member of a template class and return type why I we need to say <elemType> again.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Levent Kaya
  • 33
  • 1
  • 1
  • 7

2 Answers2

1

You should recall that class/function templates means that for each instantiation with a different template type, the compiler writes down another copy of the code, replacing elemType with the relevant type. Meaning, that if you are using in your code both SSorting<double> and SSorting<int>, you get two totally different types, with different names.

Thus, the name of the class isn't SSorting, but SSorting<elemType>!

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Kerek
  • 1,106
  • 1
  • 8
  • 18
0

Template class is not a "real" class, it is template code. It does not exist until you use it. When you do not write "elemType" part in the definition part, you're just defining a member function of "real" class.

Kor Sar
  • 51
  • 2
  • 4