About the overrided operator distinguish by c++ compiler

Submitted by Anonymous
on May 31, 2008 - 12:03am

Hi,I get a trouble when I create a class that's a seal class for string manipulate.
I override an operator ==:
bool operator ==(char *pStr);
and I override other operator (char*):
char* operator(char*)();

When I use the operator == like this:
CMyString strTest;
if(strTest == "OK")
{
return true;
}
else
{
return false;
}

I get a compile error, That mean is C++ compiler can distinguish the == operator is either(char* == char*) or (CMyString == char*).
Thanks.

class TString { public:

Anonymous (not verified)
on
May 31, 2008 - 1:10am
class TString
{
public:
  operator char *(void)
  {
    printf("(char *)\n");
    return "oups"; 
  }
  bool operator == (const char *)
  {
    printf("==(char *)\n");
    return true; 
  }
};

int main(void)
{
  TString str;
  if(str == "Foo")
    {
      printf("True\n");
    }
  else
    {
      printf("False\n");
    }
  return 0;
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.