Click to See Complete Forum and Search --> : Template functions within a Class
Lil'Hasher
February 6th, 2003, 11:53 AM
Could someone tell me what the point of something like this would be...
class Key : public KeyType
{public:
char value[20]
Key(char* newKey)
{strcpy(value, newKey);
}
Lil'Hasher
February 6th, 2003, 11:58 AM
..sorry that bit of code was not finished when i posted....i'll continue it here..
template<class KeyType>
int operator == (KeyType & right){
if(strcmp(value, right.value) ==0)
return 1;
else
return 0;
}
template <class KeyType>
int operator < (KeyType & right){
if(strcmp(value, right.value) < 0)
return 1;
else
return 0;
}
};
I dont understand what the template <class KeyType> would accomplish here. And does the KeyType refer to the existing class by the name of 'KeyType' or is it treated like a generic name like Class T , ignoring that existing class.
thanks
kuphryn
February 6th, 2003, 01:33 PM
There is an aliase flaw in your design. The template class is KeyType and it is also derived from KeyType. I assume KeyType is the name of a real class, not what you pass into the template.
template <class T>
Kuphryn
mwilliamson
February 6th, 2003, 07:01 PM
That function can take multiple classes / structures which have a public value member variable. For example, it would work with a class and all of its deriviates.
FYI, you can change you code to:
template<class KeyType>
int operator == (KeyType & right){
return strcmp(value, right.value) == 0;
}
template <class KeyType>
int operator < (KeyType & right){
return strcmp(value, right.value) < 0;
}
There is no reason to ever code
if( something == 7 )
return true;
return false;
:)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.