Hello everyone!
I have this interesting problem. Here is my code:
Code:
template <class T>
int compare (const T& a, const T& b)
{
if (a == b)
return (0);
return (a > b ? -1 : 1);
}
template <>
int compare<const char *>(const char* const & a, const char* const & b)
{
int x;
x = strcmp(a, b);
if (x == 0)
return (x);
else if (x > 0)
return (-1);
return (1);
}
template <>
int compare<char *>(char* const & a, char* const & b)
{
int x;
x = strcmp(a, b);
if (x == 0)
return (x);
else if (x > 0)
return (-1);
return (1);
}
main.cpp
Code:
class toto
{
toto &operator=(const toto&) {return *this;}
toto(const toto &){}
public:
bool operator==(const toto&) const {return true;}
bool operator>(const toto&) const {return false;}
bool operator<(const toto&) const {return false;}
toto(){}
};
int main()
{
toto a, b;
compare(a, b);
compare(1, 2);
compare(static_cast<const char*>("string1"), static_cast<const char *>("string2"));
return (0);
}
My question:
I was forced to declare one of the compare functions with (const char * const &a). I don't understand at all why... the string declared this way "string", is const char * by default, no? And even with the cast, its still const char* - so why the double const?
Thank you.